1 #include <linux/kernel.h>
2 #include <linux/kthread.h>
3 #include <linux/spinlock.h>
5 #include <linux/interrupt.h>
6 #include <linux/linkage.h>
7 #include <linux/sched.h>
8 #include <linux/uaccess.h>
9 #include <asm/irq_vectors.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kthread.h>
15 #include <asm/uaccess.h>
16 #include <linux/smp.h>
17 #include <linux/vmalloc.h>
19 #include <palacios/vmm.h>
20 #include <palacios/vmm_host_events.h>
25 #include "lockcheck.h"
27 // The following can be used to track heap bugs
28 // zero memory after allocation
29 #define ALLOC_ZERO_MEM 0
30 // pad allocations by this many bytes on both ends of block
41 static struct v3_vm_info * irq_to_guest_map[256];
44 extern unsigned int cpu_khz;
46 extern int cpu_list[NR_CPUS];
47 extern int cpu_list_len;
50 static char *print_buffer[NR_CPUS];
52 static void deinit_print_buffers(void)
56 for (i=0;i<NR_CPUS;i++) {
57 if (print_buffer[i]) {
58 palacios_free(print_buffer[i]);
64 static int init_print_buffers(void)
68 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
70 #if !V3_PRINTK_OLD_STYLE_OUTPUT
72 for (i=0;i<NR_CPUS;i++) {
73 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
74 if (!print_buffer[i]) {
75 ERROR("Cannot allocate print buffer for cpu %d\n",i);
76 deinit_print_buffers();
79 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
89 * Prints a message to the console.
91 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...) {
93 #if V3_PRINTK_OLD_STYLE_OUTPUT
107 unsigned int cpu = palacios_get_cpu();
108 struct v3_guest *guest = (struct v3_guest *)vm;
110 buf = print_buffer[cpu];
113 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
118 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
121 #if V3_PRINTK_CHECK_7BIT
125 for (i=0;i<strlen(buf);i++) {
132 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
139 printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
145 printk(KERN_INFO "palacios (pcore %u vm %s): %s",
151 printk(KERN_INFO "palacios (pcore %u): %s",
164 * Allocates a contiguous region of pages of the requested size.
165 * Returns the physical address of the first page in the region.
167 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
168 void * pg_addr = NULL;
170 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
173 ERROR("ALERT ALERT Page allocation has FAILED Warning\n");
177 pg_allocs += num_pages;
184 * Frees a page previously allocated via palacios_allocate_page().
185 * Note that palacios_allocate_page() can allocate multiple pages with
186 * a single call while palacios_free_page() only frees a single page.
189 void palacios_free_pages(void * page_paddr, int num_pages) {
190 pg_frees += num_pages;
191 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
196 palacios_alloc_extended(unsigned int size, unsigned int flags) {
199 addr = kmalloc(size+2*ALLOC_PAD, flags);
202 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
209 memset(addr,0,size+2*ALLOC_PAD);
212 return addr+ALLOC_PAD;
216 palacios_valloc(unsigned int size)
220 addr = vmalloc(size);
223 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
232 void palacios_vfree(void *p)
239 * Allocates 'size' bytes of kernel memory.
240 * Returns the kernel virtual address of the memory allocated.
243 palacios_alloc(unsigned int size) {
245 // It is very important that this test remains since
246 // this function is used extensively throughout palacios and the linux
247 // module, both in places where interrupts are off and where they are on
248 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
249 if (irqs_disabled()) {
250 return palacios_alloc_extended(size,GFP_ATOMIC);
252 return palacios_alloc_extended(size,GFP_KERNEL);
258 * Frees memory that was previously allocated by palacios_alloc().
266 kfree(addr-ALLOC_PAD);
271 * Converts a kernel virtual address to the corresponding physical address.
274 palacios_vaddr_to_paddr(
278 return (void*) __pa(vaddr);
283 * Converts a physical address to the corresponding kernel virtual address.
286 palacios_paddr_to_vaddr(
294 * Runs a function on the specified CPU.
299 void (*fn)(void *arg),
305 // We set wait to 1, but I'm not sure this is necessary
306 smp_call_function_single(cpu_id, fn, arg, 1);
312 #define MAX_THREAD_NAME 32
314 struct lnx_thread_arg {
315 int (*fn)(void * arg);
317 char name[MAX_THREAD_NAME];
320 static int lnx_thread_target(void * arg) {
321 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
324 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
326 daemonize(thread_info->name);
327 allow_signal(SIGKILL);
331 ret = thread_info->fn(thread_info->arg);
334 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
336 palacios_free(thread_info);
341 return 0; // should not get here.
345 * Creates a kernel thread.
348 palacios_start_kernel_thread(
349 int (*fn) (void * arg),
351 char * thread_name) {
353 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
356 ERROR("ALERT ALERT Unable to allocate thread\n");
360 thread_info->fn = fn;
361 thread_info->arg = arg;
362 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
363 thread_info->name[MAX_THREAD_NAME-1] =0;
365 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
370 * Starts a kernel thread on the specified CPU.
373 palacios_start_thread_on_cpu(int cpu_id,
374 int (*fn)(void * arg),
376 char * thread_name ) {
377 struct task_struct * thread = NULL;
378 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
381 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
385 thread_info->fn = fn;
386 thread_info->arg = arg;
387 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
388 thread_info->name[MAX_THREAD_NAME-1] =0;
390 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
392 if (IS_ERR(thread)) {
393 WARNING("Palacios error creating thread: %s\n", thread_info->name);
394 palacios_free(thread_info);
398 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
399 WARNING("Attempt to start thread on disallowed CPU\n");
400 kthread_stop(thread);
401 palacios_free(thread_info);
405 wake_up_process(thread);
412 * Rebind a kernel thread to the specified CPU
413 * The thread will be running on target CPU on return
414 * non-zero return means failure
417 palacios_move_thread_to_cpu(int new_cpu_id,
419 struct task_struct * thread = (struct task_struct *)thread_ptr;
421 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
423 if (thread == NULL) {
428 * Bind to the specified CPU. When this call returns,
429 * the thread should be running on the target CPU.
431 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
436 * Returns the CPU ID that the caller is running on.
439 palacios_get_cpu(void)
442 /* We want to call smp_processor_id()
443 * But this is not safe if kernel preemption is possible
444 * We need to ensure that the palacios threads are bound to a give cpu
447 unsigned int cpu_id = get_cpu();
453 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
456 * This is dependent on the implementation of xcall_reschedule(). Currently
457 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
458 * but instead relies on the return to user space to handle it. Because
459 * palacios is a kernel thread schedule will not be called, which is correct.
460 * If it ever changes to induce side effects, we'll need to figure something
464 #include <asm/apic.h>
467 palacios_interrupt_cpu(
468 struct v3_vm_info * vm,
474 smp_send_reschedule(cpu_id);
476 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
482 * Dispatches an interrupt to Palacios for handling.
485 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
486 struct v3_interrupt intr = {
488 .error = regs->orig_ax,
492 if (irq_to_guest_map[vector]) {
493 v3_deliver_irq(irq_to_guest_map[vector], &intr);
499 * Instructs the kernel to forward the specified IRQ to Palacios.
502 palacios_hook_interrupt(struct v3_vm_info * vm,
503 unsigned int vector ) {
504 INFO("hooking vector %d\n", vector);
506 if (irq_to_guest_map[vector]) {
508 "%s: Interrupt vector %u is already hooked.\n",
514 "%s: Hooking interrupt vector %u to vm %p.\n",
515 __func__, vector, vm);
517 irq_to_guest_map[vector] = vm;
520 * NOTE: Normally PCI devices are supposed to be level sensitive,
521 * but we need them to be edge sensitive so that they are
522 * properly latched by Palacios. Leaving them as level
523 * sensitive would lead to an interrupt storm.
525 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
527 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
529 ERROR("unexpected vector for hooking\n");
537 DEBUG("hooking vector: %d\n", vector);
545 error = request_irq((vector - 32),
546 (void *)palacios_dispatch_interrupt,
548 "interrupt_for_palacios",
552 ERROR("error code for request_irq is %d\n", error);
553 ERROR("request vector %d failed", vector);
564 * Acknowledges an interrupt.
567 palacios_ack_interrupt(
572 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
577 * Returns the CPU frequency in kilohertz.
580 palacios_get_cpu_khz(void)
582 INFO("cpu_khz is %u\n", cpu_khz);
585 INFO("faking cpu_khz to 1000000\n");
594 * Yield the CPU so other host OS tasks can run.
595 * This will return immediately if there is no other thread that is runnable
596 * And there is no real bound on how long it will yield
599 palacios_yield_cpu(void)
606 * Yield the CPU so other host OS tasks can run.
607 * Given now immediately if there is no other thread that is runnable
608 * And there is no real bound on how long it will yield
610 void palacios_sleep_cpu(unsigned int us)
613 set_current_state(TASK_INTERRUPTIBLE);
615 unsigned int uspj = 1000000U/HZ;
616 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
617 schedule_timeout(jiffies);
624 void palacios_wakeup_cpu(void *thread)
626 wake_up_process(thread);
632 * Returns NULL on failure.
635 palacios_mutex_alloc(void)
637 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
640 spin_lock_init(lock);
641 LOCKCHECK_ALLOC(lock);
643 ERROR("ALERT ALERT Unable to allocate lock\n");
650 void palacios_mutex_init(void *mutex)
652 spinlock_t *lock = (spinlock_t*)mutex;
655 spin_lock_init(lock);
656 LOCKCHECK_ALLOC(lock);
665 palacios_mutex_free(void * mutex) {
666 palacios_free(mutex);
667 LOCKCHECK_FREE(mutex);
674 palacios_mutex_lock(void * mutex, int must_spin) {
675 spin_lock((spinlock_t *)mutex);
676 LOCKCHECK_LOCK(mutex);
681 * Locks a mutex, disabling interrupts on this core
684 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
688 spin_lock_irqsave((spinlock_t *)mutex,flags);
689 LOCKCHECK_LOCK_IRQSAVE(mutex,flags);
691 return (void *)flags;
699 palacios_mutex_unlock(
703 spin_unlock((spinlock_t *)mutex);
704 LOCKCHECK_UNLOCK(mutex);
709 * Unlocks a mutex and restores previous interrupt state on this core
712 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
714 // This is correct, flags is opaque
715 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
716 LOCKCHECK_UNLOCK_IRQRESTORE(mutex,(unsigned long)flags);
720 * Structure used by the Palacios hypervisor to interface with the host kernel.
722 static struct v3_os_hooks palacios_os_hooks = {
723 .print = palacios_print_scoped,
724 .allocate_pages = palacios_allocate_pages,
725 .free_pages = palacios_free_pages,
726 .malloc = palacios_alloc,
727 .free = palacios_free,
728 .vaddr_to_paddr = palacios_vaddr_to_paddr,
729 .paddr_to_vaddr = palacios_paddr_to_vaddr,
730 .hook_interrupt = palacios_hook_interrupt,
731 .ack_irq = palacios_ack_interrupt,
732 .get_cpu_khz = palacios_get_cpu_khz,
733 .start_kernel_thread = palacios_start_kernel_thread,
734 .yield_cpu = palacios_yield_cpu,
735 .sleep_cpu = palacios_sleep_cpu,
736 .wakeup_cpu = palacios_wakeup_cpu,
737 .mutex_alloc = palacios_mutex_alloc,
738 .mutex_free = palacios_mutex_free,
739 .mutex_lock = palacios_mutex_lock,
740 .mutex_unlock = palacios_mutex_unlock,
741 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
742 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
743 .get_cpu = palacios_get_cpu,
744 .interrupt_cpu = palacios_interrupt_cpu,
745 .call_on_cpu = palacios_xcall,
746 .start_thread_on_cpu = palacios_start_thread_on_cpu,
747 .move_thread_to_cpu = palacios_move_thread_to_cpu,
753 int palacios_vmm_init( char *options )
755 int num_cpus = num_online_cpus();
756 char * cpu_mask = NULL;
758 if (cpu_list_len > 0) {
763 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
766 ERROR("Cannot allocate cpu mask\n");
770 memset(cpu_mask, 0, (num_cpus / 8) + 1);
772 for (i = 0; i < cpu_list_len; i++) {
773 if (cpu_list[i] >= num_cpus) {
774 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
778 major = cpu_list[i] / 8;
779 minor = cpu_list[i] % 8;
781 *(cpu_mask + major) |= (0x1 << minor);
785 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
787 if (init_print_buffers()) {
788 ERROR("Cannot initialize print buffers\n");
789 palacios_free(cpu_mask);
793 INFO("palacios_init starting - calling init_v3\n");
795 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
802 int palacios_vmm_exit( void ) {
806 INFO("palacios shutdown complete\n");
808 deinit_print_buffers();