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-inspector.h"
35 #ifdef V3_CONFIG_KEYED_STREAMS
36 #include "palacios-keyed-stream.h"
40 MODULE_LICENSE("GPL");
46 static int v3_major_num = 0;
48 static u8 v3_minor_map[MAX_VMS / 8] = {[0 ... (MAX_VMS / 8) - 1] = 0};
51 struct class * v3_class = NULL;
52 static struct cdev ctrl_dev;
54 void * v3_base_addr = NULL;
55 unsigned int v3_pages = 0;
57 static int register_vm( void ) {
61 for (i = 0; i < sizeof(v3_minor_map); i++) {
62 if (v3_minor_map[i] != 0xff) {
63 for (j = 0; j < 8; j++) {
64 if (!v3_minor_map[i] & (0x1 << j)) {
66 v3_minor_map[i] |= (0x1 << j);
86 static long v3_dev_ioctl(struct file * filp,
87 unsigned int ioctl, unsigned long arg) {
88 void __user * argp = (void __user *)arg;
89 printk("V3 IOCTL %d\n", ioctl);
95 struct v3_guest_img user_image;
96 struct v3_guest * guest = kmalloc(sizeof(struct v3_guest), GFP_KERNEL);
99 printk("Error allocating Kernel guest_image\n");
103 memset(guest, 0, sizeof(struct v3_guest));
105 printk("Starting V3 Guest...\n");
107 vm_minor = register_vm();
109 if (vm_minor == -1) {
110 printk("Too many VMs are currently running\n");
114 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
116 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
117 printk("copy from user error getting guest image...\n");
121 guest->img_size = user_image.size;
123 printk("Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
124 guest->img = kmalloc(guest->img_size, GFP_KERNEL);
126 if (IS_ERR(guest->img)) {
127 printk("Error: Could not allocate space for guest image\n");
131 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
132 printk("Error loading guest data\n");
136 strncpy(guest->name, user_image.name, 127);
138 printk("Launching VM\n");
140 INIT_LIST_HEAD(&(guest->streams));
141 INIT_LIST_HEAD(&(guest->files));
142 INIT_LIST_HEAD(&(guest->sockets));
143 #ifdef V3_CONFIG_HOST_DEVICE
144 INIT_LIST_HEAD(&(guest->hostdev.devs));
146 init_completion(&(guest->start_done));
147 init_completion(&(guest->thread_done));
150 struct task_struct * launch_thread = NULL;
151 // At some point we're going to want to allow the user to specify a CPU mask
152 // But for now, well just launch from the local core, and rely on the global cpu mask
155 launch_thread = kthread_create(start_palacios_vm, guest, guest->name);
157 if (IS_ERR(launch_thread)) {
159 printk("Palacios error creating launch thread for vm (%s)\n", guest->name);
163 kthread_bind(launch_thread, smp_processor_id());
166 wake_up_process(launch_thread);
169 wait_for_completion(&(guest->start_done));
171 return guest->vm_dev;
174 case V3_ADD_MEMORY: {
175 struct v3_mem_region mem;
177 memset(&mem, 0, sizeof(struct v3_mem_region));
179 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
180 printk("copy from user error getting mem_region...\n");
184 printk("Adding %llu pages to Palacios memory\n", mem.num_pages);
186 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
187 printk("Error adding memory to Palacios\n");
195 printk("\tUnhandled\n");
204 static struct file_operations v3_ctrl_fops = {
205 .owner = THIS_MODULE,
206 .unlocked_ioctl = v3_dev_ioctl,
207 .compat_ioctl = v3_dev_ioctl,
211 extern unsigned int v3_pages;
212 extern void * v3_base_addr;
214 static int __init v3_init(void) {
215 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
221 v3_class = class_create(THIS_MODULE, "vms");
222 if (IS_ERR(v3_class)) {
223 printk("Failed to register V3 VM device class\n");
224 return PTR_ERR(v3_class);
227 printk("intializing V3 Control device\n");
229 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
232 printk("Error registering device region for V3 devices\n");
236 v3_major_num = MAJOR(dev);
238 dev = MKDEV(v3_major_num, MAX_VMS + 1);
241 printk("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
242 cdev_init(&ctrl_dev, &v3_ctrl_fops);
243 ctrl_dev.owner = THIS_MODULE;
244 ctrl_dev.ops = &v3_ctrl_fops;
245 cdev_add(&ctrl_dev, dev, 1);
247 device_create(v3_class, NULL, dev, NULL, "v3vee");
250 printk("Error adding v3 control device\n");
254 if ((v3_pages > 0) && (v3_base_addr != NULL)) {
255 add_palacios_memory(__pa(v3_base_addr), v3_pages);
258 // Initialize Palacios
262 #ifdef V3_CONFIG_STREAM
263 palacios_init_stream();
266 #ifdef V3_CONFIG_FILE
267 palacios_file_init();
270 #ifdef V3_CONFIG_KEYED_STREAMS
271 palacios_init_keyed_streams();
274 #ifdef V3_CONFIG_CONSOLE
275 palacios_init_console();
278 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
279 palacios_init_graphics_console();
282 #ifdef V3_CONFIG_EXT_INSPECTOR
283 palacios_init_inspector();
286 #ifdef V3_CONFIG_SOCKET
287 palacios_socket_init();
290 #ifdef V3_CONFIG_PACKET
291 palacios_init_packet(NULL);
294 #ifdef V3_CONFIG_VNET
295 palacios_vnet_init();
298 #ifdef V3_CONFIG_HOST_DEVICE
299 palacios_init_host_dev();
305 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
307 class_destroy(v3_class);
313 static void __exit v3_exit(void) {
314 extern u32 pg_allocs;
320 // should probably try to stop any guests
324 dev_t dev = MKDEV(v3_major_num, MAX_VMS + 1);
326 printk("Removing V3 Control device\n");
331 printk("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
332 printk("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
334 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
338 device_destroy(v3_class, dev);
339 class_destroy(v3_class);
343 #ifdef V3_CONFIG_EXT_INSPECTOR
344 palacios_deinit_inspector();
347 #ifdef V3_CONFIG_FILE
348 palacios_file_deinit();
351 #ifdef V3_CONFIG_STREAM
352 palacios_deinit_stream();
355 #ifdef V3_CONFIG_SOCKET
356 palacios_socket_deinit();
359 #ifdef V3_CONFIG_PACKET
360 palacios_deinit_packet(NULL);
363 #ifdef V3_CONFIG_VNET
364 palacios_vnet_deinit();
367 palacios_deinit_mm();
369 printk("Palacios Module Mallocs = %d, Frees = %d\n", mod_allocs, mod_frees);
374 module_init(v3_init);
375 module_exit(v3_exit);
379 void * trace_malloc(size_t size, gfp_t flags) {
383 addr = kmalloc(size, flags);
389 void trace_free(const void * objp) {