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");
31 static int v3_major_num = 0;
33 static u8 v3_minor_map[MAX_VMS / 8] = {[0 ... (MAX_VMS / 8) - 1] = 0};
36 struct class * v3_class = NULL;
37 static struct cdev ctrl_dev;
39 void * v3_base_addr = NULL;
40 unsigned int v3_pages = 0;
42 static int register_vm( void ) {
46 for (i = 0; i < sizeof(v3_minor_map); i++) {
47 if (v3_minor_map[i] != 0xff) {
48 for (j = 0; j < 8; j++) {
49 if (!v3_minor_map[i] & (0x1 << j)) {
51 v3_minor_map[i] |= (0x1 << j);
71 static long v3_dev_ioctl(struct file * filp,
72 unsigned int ioctl, unsigned long arg) {
73 void __user * argp = (void __user *)arg;
74 printk("V3 IOCTL %d\n", ioctl);
79 struct v3_guest_img user_image;
80 struct v3_guest * guest = kmalloc(sizeof(struct v3_guest), GFP_KERNEL);
84 printk("Error allocating Kernel guest_image\n");
88 memset(guest, 0, sizeof(struct v3_guest));
90 printk("Starting V3 Guest...\n");
92 vm_minor = register_vm();
95 printk("Too many VMs are currently running\n");
99 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
101 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
102 printk("copy from user error getting guest image...\n");
106 guest->img_size = user_image.size;
108 printk("Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
109 guest->img = kmalloc(guest->img_size, GFP_KERNEL);
111 if (IS_ERR(guest->img)) {
112 printk("Error: Could not allocate space for guest image\n");
116 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
117 printk("Error loading guest data\n");
121 strncpy(guest->name, user_image.name, 127);
123 printk("Launching VM\n");
125 INIT_LIST_HEAD(&(guest->streams));
126 INIT_LIST_HEAD(&(guest->files));
127 INIT_LIST_HEAD(&(guest->sockets));
128 init_completion(&(guest->thread_done));
130 kthread_run(start_palacios_vm, guest, guest->name);
132 wait_for_completion(&(guest->thread_done));
134 return guest->vm_dev;
137 case V3_ADD_MEMORY: {
138 struct v3_mem_region mem;
140 memset(&mem, 0, sizeof(struct v3_mem_region));
142 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
143 printk("copy from user error getting mem_region...\n");
147 printk("Adding %llu pages to Palacios memory\n", mem.num_pages);
149 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
150 printk("Error adding memory to Palacios\n");
157 void * vaddr = __va(alloc_palacios_pgs(131072, 4096));
158 memset(vaddr, 0xfe492fe2, mem.num_pages * 4096);
165 printk("\tUnhandled\n");
174 static struct file_operations v3_ctrl_fops = {
175 .owner = THIS_MODULE,
176 .unlocked_ioctl = v3_dev_ioctl,
177 .compat_ioctl = v3_dev_ioctl,
181 extern unsigned int v3_pages;
182 extern void * v3_base_addr;
184 static int __init v3_init(void) {
185 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
191 v3_class = class_create(THIS_MODULE, "vms");
192 if (IS_ERR(v3_class)) {
193 printk("Failed to register V3 VM device class\n");
194 return PTR_ERR(v3_class);
197 printk("intializing V3 Control device\n");
199 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
202 printk("Error registering device region for V3 devices\n");
206 v3_major_num = MAJOR(dev);
208 dev = MKDEV(v3_major_num, MAX_VMS + 1);
211 printk("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
212 cdev_init(&ctrl_dev, &v3_ctrl_fops);
213 ctrl_dev.owner = THIS_MODULE;
214 ctrl_dev.ops = &v3_ctrl_fops;
215 cdev_add(&ctrl_dev, dev, 1);
217 device_create(v3_class, NULL, dev, NULL, "v3vee");
220 printk("Error adding v3 control device\n");
224 if ((v3_pages > 0) && (v3_base_addr != NULL)) {
225 add_palacios_memory(__pa(v3_base_addr), v3_pages);
228 // Initialize Palacios
232 palacios_init_stream();
233 palacios_file_init();
234 palacios_init_console();
241 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
243 class_destroy(v3_class);
248 static void __exit v3_exit(void) {
249 extern u32 pg_allocs;
254 printk("Removing V3 Control device\n");
259 printk("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
260 printk("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
263 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
268 module_init(v3_init);
269 module_exit(v3_exit);