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>
22 #include "palacios-mm.h"
23 #include "palacios-vm.h"
24 #include "palacios-stream.h"
25 #include "palacios-file.h"
26 #include "palacios-serial.h"
28 MODULE_LICENSE("GPL");
34 static int v3_major_num = 0;
36 static u8 v3_minor_map[MAX_VMS / 8] = {[0 ... (MAX_VMS / 8) - 1] = 0};
39 struct class * v3_class = NULL;
40 static struct cdev ctrl_dev;
42 void * v3_base_addr = NULL;
43 unsigned int v3_pages = 0;
45 static int register_vm( void ) {
49 for (i = 0; i < sizeof(v3_minor_map); i++) {
50 if (v3_minor_map[i] != 0xff) {
51 for (j = 0; j < 8; j++) {
52 if (!v3_minor_map[i] & (0x1 << j)) {
54 v3_minor_map[i] |= (0x1 << j);
74 static long v3_dev_ioctl(struct file * filp,
75 unsigned int ioctl, unsigned long arg) {
76 void __user * argp = (void __user *)arg;
77 printk("V3 IOCTL %d\n", ioctl);
83 struct v3_guest_img user_image;
84 struct v3_guest * guest = kmalloc(sizeof(struct v3_guest), GFP_KERNEL);
87 printk("Error allocating Kernel guest_image\n");
91 memset(guest, 0, sizeof(struct v3_guest));
93 printk("Starting V3 Guest...\n");
95 vm_minor = register_vm();
98 printk("Too many VMs are currently running\n");
102 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
104 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
105 printk("copy from user error getting guest image...\n");
109 guest->img_size = user_image.size;
111 printk("Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
112 guest->img = kmalloc(guest->img_size, GFP_KERNEL);
114 if (IS_ERR(guest->img)) {
115 printk("Error: Could not allocate space for guest image\n");
119 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
120 printk("Error loading guest data\n");
124 strncpy(guest->name, user_image.name, 127);
126 printk("Launching VM\n");
128 INIT_LIST_HEAD(&(guest->streams));
129 INIT_LIST_HEAD(&(guest->files));
130 INIT_LIST_HEAD(&(guest->sockets));
131 init_completion(&(guest->start_done));
132 init_completion(&(guest->thread_done));
134 kthread_run(start_palacios_vm, guest, guest->name);
136 wait_for_completion(&(guest->start_done));
138 return guest->vm_dev;
141 case V3_ADD_MEMORY: {
142 struct v3_mem_region mem;
144 memset(&mem, 0, sizeof(struct v3_mem_region));
146 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
147 printk("copy from user error getting mem_region...\n");
151 printk("Adding %llu pages to Palacios memory\n", mem.num_pages);
153 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
154 printk("Error adding memory to Palacios\n");
161 void * vaddr = __va(alloc_palacios_pgs(131072, 4096));
162 memset(vaddr, 0xfe492fe2, mem.num_pages * 4096);
169 printk("\tUnhandled\n");
178 static struct file_operations v3_ctrl_fops = {
179 .owner = THIS_MODULE,
180 .unlocked_ioctl = v3_dev_ioctl,
181 .compat_ioctl = v3_dev_ioctl,
185 extern unsigned int v3_pages;
186 extern void * v3_base_addr;
188 static int __init v3_init(void) {
189 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
195 v3_class = class_create(THIS_MODULE, "vms");
196 if (IS_ERR(v3_class)) {
197 printk("Failed to register V3 VM device class\n");
198 return PTR_ERR(v3_class);
201 printk("intializing V3 Control device\n");
203 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
206 printk("Error registering device region for V3 devices\n");
210 v3_major_num = MAJOR(dev);
212 dev = MKDEV(v3_major_num, MAX_VMS + 1);
215 printk("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
216 cdev_init(&ctrl_dev, &v3_ctrl_fops);
217 ctrl_dev.owner = THIS_MODULE;
218 ctrl_dev.ops = &v3_ctrl_fops;
219 cdev_add(&ctrl_dev, dev, 1);
221 device_create(v3_class, NULL, dev, NULL, "v3vee");
224 printk("Error adding v3 control device\n");
228 if ((v3_pages > 0) && (v3_base_addr != NULL)) {
229 add_palacios_memory(__pa(v3_base_addr), v3_pages);
232 // Initialize Palacios
236 palacios_init_stream();
237 palacios_file_init();
238 palacios_init_console();
243 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
245 class_destroy(v3_class);
251 static void __exit v3_exit(void) {
252 extern u32 pg_allocs;
258 // should probably try to stop any guests
262 dev_t dev = MKDEV(v3_major_num, MAX_VMS + 1);
264 printk("Removing V3 Control device\n");
269 printk("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
270 printk("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
272 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
276 device_destroy(v3_class, dev);
277 class_destroy(v3_class);
280 palacios_file_deinit();
281 palacios_deinit_stream();
283 palacios_deinit_mm();
285 printk("Palacios Module Mallocs = %d, Frees = %d\n", mod_allocs, mod_frees);
290 module_init(v3_init);
291 module_exit(v3_exit);
295 void * trace_malloc(size_t size, gfp_t flags) {
299 addr = kmalloc(size, flags);
305 void trace_free(const void * objp) {