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"
27 #include "palacios-socket.h"
28 #include "palacios-vnet.h"
29 #include "palacios-packet.h"
31 #ifdef V3_CONFIG_EXT_INSPECTOR
32 #include "palacios-debugfs.h"
35 MODULE_LICENSE("GPL");
41 static int v3_major_num = 0;
43 static u8 v3_minor_map[MAX_VMS / 8] = {[0 ... (MAX_VMS / 8) - 1] = 0};
46 struct class * v3_class = NULL;
47 static struct cdev ctrl_dev;
49 void * v3_base_addr = NULL;
50 unsigned int v3_pages = 0;
52 static int register_vm( void ) {
56 for (i = 0; i < sizeof(v3_minor_map); i++) {
57 if (v3_minor_map[i] != 0xff) {
58 for (j = 0; j < 8; j++) {
59 if (!v3_minor_map[i] & (0x1 << j)) {
61 v3_minor_map[i] |= (0x1 << j);
81 static long v3_dev_ioctl(struct file * filp,
82 unsigned int ioctl, unsigned long arg) {
83 void __user * argp = (void __user *)arg;
84 printk("V3 IOCTL %d\n", ioctl);
90 struct v3_guest_img user_image;
91 struct v3_guest * guest = kmalloc(sizeof(struct v3_guest), GFP_KERNEL);
94 printk("Error allocating Kernel guest_image\n");
98 memset(guest, 0, sizeof(struct v3_guest));
100 printk("Starting V3 Guest...\n");
102 vm_minor = register_vm();
104 if (vm_minor == -1) {
105 printk("Too many VMs are currently running\n");
109 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
111 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
112 printk("copy from user error getting guest image...\n");
116 guest->img_size = user_image.size;
118 printk("Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
119 guest->img = kmalloc(guest->img_size, GFP_KERNEL);
121 if (IS_ERR(guest->img)) {
122 printk("Error: Could not allocate space for guest image\n");
126 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
127 printk("Error loading guest data\n");
131 strncpy(guest->name, user_image.name, 127);
133 printk("Launching VM\n");
135 INIT_LIST_HEAD(&(guest->streams));
136 INIT_LIST_HEAD(&(guest->files));
137 INIT_LIST_HEAD(&(guest->sockets));
138 init_completion(&(guest->start_done));
139 init_completion(&(guest->thread_done));
141 kthread_run(start_palacios_vm, guest, guest->name);
143 wait_for_completion(&(guest->start_done));
145 return guest->vm_dev;
148 case V3_ADD_MEMORY: {
149 struct v3_mem_region mem;
151 memset(&mem, 0, sizeof(struct v3_mem_region));
153 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
154 printk("copy from user error getting mem_region...\n");
158 printk("Adding %llu pages to Palacios memory\n", mem.num_pages);
160 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
161 printk("Error adding memory to Palacios\n");
168 void * vaddr = __va(alloc_palacios_pgs(131072, 4096));
169 memset(vaddr, 0xfe492fe2, mem.num_pages * 4096);
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,
193 extern unsigned int v3_pages;
194 extern void * v3_base_addr;
196 static int __init v3_init(void) {
197 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
203 v3_class = class_create(THIS_MODULE, "vms");
204 if (IS_ERR(v3_class)) {
205 printk("Failed to register V3 VM device class\n");
206 return PTR_ERR(v3_class);
209 printk("intializing V3 Control device\n");
211 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
214 printk("Error registering device region for V3 devices\n");
218 v3_major_num = MAJOR(dev);
220 dev = MKDEV(v3_major_num, MAX_VMS + 1);
223 printk("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
224 cdev_init(&ctrl_dev, &v3_ctrl_fops);
225 ctrl_dev.owner = THIS_MODULE;
226 ctrl_dev.ops = &v3_ctrl_fops;
227 cdev_add(&ctrl_dev, dev, 1);
229 device_create(v3_class, NULL, dev, NULL, "v3vee");
232 printk("Error adding v3 control device\n");
236 if ((v3_pages > 0) && (v3_base_addr != NULL)) {
237 add_palacios_memory(__pa(v3_base_addr), v3_pages);
240 // Initialize Palacios
244 #ifdef V3_CONFIG_STREAM
245 palacios_init_stream();
248 #ifdef V3_CONFIG_FILE
249 palacios_file_init();
252 #ifdef V3_CONFIG_CONSOLE
253 palacios_init_console();
256 #ifdef V3_CONFIG_INSPECTOR
257 palacios_init_debugfs();
260 #ifdef V3_CONFIG_SOCKET
261 palacios_socket_init();
264 #ifdef V3_CONFIG_PACKET
265 palacios_init_packet(NULL);
268 #ifdef V3_CONFIG_VNET
269 palacios_init_vnet();
275 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
277 class_destroy(v3_class);
283 static void __exit v3_exit(void) {
284 extern u32 pg_allocs;
290 // should probably try to stop any guests
294 dev_t dev = MKDEV(v3_major_num, MAX_VMS + 1);
296 printk("Removing V3 Control device\n");
301 printk("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
302 printk("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
304 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
308 device_destroy(v3_class, dev);
309 class_destroy(v3_class);
313 #ifdef V3_CONFIG_EXT_INSPECTOR
314 palacios_deinit_debugfs();
317 #ifdef V3_CONFIG_FILE
318 palacios_file_deinit();
321 #ifdef V3_CONFIG_STREAM
322 palacios_deinit_stream();
325 palacios_deinit_mm();
327 printk("Palacios Module Mallocs = %d, Frees = %d\n", mod_allocs, mod_frees);
332 module_init(v3_init);
333 module_exit(v3_exit);
337 void * trace_malloc(size_t size, gfp_t flags) {
341 addr = kmalloc(size, flags);
347 void trace_free(const void * objp) {