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>
26 #include "lockcheck.h"
28 // The following can be used to track heap bugs
29 // zero memory after allocation
30 #define ALLOC_ZERO_MEM 0
31 // pad allocations by this many bytes on both ends of block
42 static struct v3_vm_info * irq_to_guest_map[256];
45 extern unsigned int cpu_khz;
47 extern int cpu_list[NR_CPUS];
48 extern int cpu_list_len;
51 static char *print_buffer[NR_CPUS];
53 static void deinit_print_buffers(void)
57 for (i=0;i<NR_CPUS;i++) {
58 if (print_buffer[i]) {
59 palacios_free(print_buffer[i]);
65 static int init_print_buffers(void)
69 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
71 #if !V3_PRINTK_OLD_STYLE_OUTPUT
73 for (i=0;i<NR_CPUS;i++) {
74 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
75 if (!print_buffer[i]) {
76 ERROR("Cannot allocate print buffer for cpu %d\n",i);
77 deinit_print_buffers();
80 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
90 * Prints a message to the console.
92 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...) {
94 #if V3_PRINTK_OLD_STYLE_OUTPUT
108 unsigned int cpu = palacios_get_cpu();
109 struct v3_guest *guest = (struct v3_guest *)vm;
111 buf = print_buffer[cpu];
114 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
119 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
122 #if V3_PRINTK_CHECK_7BIT
126 for (i=0;i<strlen(buf);i++) {
133 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
140 printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
146 printk(KERN_INFO "palacios (pcore %u vm %s): %s",
152 printk(KERN_INFO "palacios (pcore %u): %s",
165 * Allocates a contiguous region of pages of the requested size.
166 * Returns the physical address of the first page in the region.
168 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
169 void * pg_addr = NULL;
172 ERROR("ALERT ALERT Attempt to allocate zero or fewer pages\n");
176 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
179 ERROR("ALERT ALERT Page allocation has FAILED Warning\n");
183 pg_allocs += num_pages;
185 MEMCHECK_ALLOC_PAGES(pg_addr,num_pages*4096);
192 * Frees a page previously allocated via palacios_allocate_page().
193 * Note that palacios_allocate_page() can allocate multiple pages with
194 * a single call while palacios_free_page() only frees a single page.
197 void palacios_free_pages(void * page_paddr, int num_pages) {
198 pg_frees += num_pages;
199 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
200 MEMCHECK_FREE_PAGES(page_paddr,num_pages*4096);
206 palacios_alloc_extended(unsigned int size, unsigned int flags) {
210 // note that modern kernels will respond to a zero byte
211 // kmalloc and return the address 0x10... In Palacios,
212 // we will simply not allow 0 byte allocs at all, of any kind
213 ERROR("ALERT ALERT attempt to kmalloc zero bytes rejected\n");
217 addr = kmalloc(size+2*ALLOC_PAD, flags);
220 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
227 memset(addr,0,size+2*ALLOC_PAD);
230 MEMCHECK_KMALLOC(addr,size+2*ALLOC_PAD);
232 return addr+ALLOC_PAD;
236 palacios_valloc(unsigned int size)
241 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
245 addr = vmalloc(size);
248 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
254 MEMCHECK_VMALLOC(addr,size);
259 void palacios_vfree(void *p)
267 * Allocates 'size' bytes of kernel memory.
268 * Returns the kernel virtual address of the memory allocated.
271 palacios_alloc(unsigned int size) {
273 // It is very important that this test remains since
274 // this function is used extensively throughout palacios and the linux
275 // module, both in places where interrupts are off and where they are on
276 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
277 if (irqs_disabled()) {
278 return palacios_alloc_extended(size,GFP_ATOMIC);
280 return palacios_alloc_extended(size,GFP_KERNEL);
286 * Frees memory that was previously allocated by palacios_alloc().
294 kfree(addr-ALLOC_PAD);
295 MEMCHECK_KFREE(addr-ALLOC_PAD);
299 * Converts a kernel virtual address to the corresponding physical address.
302 palacios_vaddr_to_paddr(
306 return (void*) __pa(vaddr);
311 * Converts a physical address to the corresponding kernel virtual address.
314 palacios_paddr_to_vaddr(
322 * Runs a function on the specified CPU.
327 void (*fn)(void *arg),
333 // We set wait to 1, but I'm not sure this is necessary
334 smp_call_function_single(cpu_id, fn, arg, 1);
340 #define MAX_THREAD_NAME 32
342 struct lnx_thread_arg {
343 int (*fn)(void * arg);
345 char name[MAX_THREAD_NAME];
348 static int lnx_thread_target(void * arg) {
349 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
352 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
354 daemonize(thread_info->name);
355 allow_signal(SIGKILL);
359 ret = thread_info->fn(thread_info->arg);
362 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
364 palacios_free(thread_info);
369 return 0; // should not get here.
373 * Creates a kernel thread.
376 palacios_start_kernel_thread(
377 int (*fn) (void * arg),
379 char * thread_name) {
381 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
384 ERROR("ALERT ALERT Unable to allocate thread\n");
388 thread_info->fn = fn;
389 thread_info->arg = arg;
390 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
391 thread_info->name[MAX_THREAD_NAME-1] =0;
393 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
398 * Starts a kernel thread on the specified CPU.
401 palacios_start_thread_on_cpu(int cpu_id,
402 int (*fn)(void * arg),
404 char * thread_name ) {
405 struct task_struct * thread = NULL;
406 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
409 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
413 thread_info->fn = fn;
414 thread_info->arg = arg;
415 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
416 thread_info->name[MAX_THREAD_NAME-1] =0;
418 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
420 if (IS_ERR(thread)) {
421 WARNING("Palacios error creating thread: %s\n", thread_info->name);
422 palacios_free(thread_info);
426 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
427 WARNING("Attempt to start thread on disallowed CPU\n");
428 kthread_stop(thread);
429 palacios_free(thread_info);
433 wake_up_process(thread);
440 * Rebind a kernel thread to the specified CPU
441 * The thread will be running on target CPU on return
442 * non-zero return means failure
445 palacios_move_thread_to_cpu(int new_cpu_id,
447 struct task_struct * thread = (struct task_struct *)thread_ptr;
449 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
451 if (thread == NULL) {
456 * Bind to the specified CPU. When this call returns,
457 * the thread should be running on the target CPU.
459 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
464 * Returns the CPU ID that the caller is running on.
467 palacios_get_cpu(void)
470 /* We want to call smp_processor_id()
471 * But this is not safe if kernel preemption is possible
472 * We need to ensure that the palacios threads are bound to a give cpu
475 unsigned int cpu_id = get_cpu();
481 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
484 * This is dependent on the implementation of xcall_reschedule(). Currently
485 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
486 * but instead relies on the return to user space to handle it. Because
487 * palacios is a kernel thread schedule will not be called, which is correct.
488 * If it ever changes to induce side effects, we'll need to figure something
492 #include <asm/apic.h>
495 palacios_interrupt_cpu(
496 struct v3_vm_info * vm,
502 smp_send_reschedule(cpu_id);
504 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
510 * Dispatches an interrupt to Palacios for handling.
513 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
514 struct v3_interrupt intr = {
516 .error = regs->orig_ax,
520 if (irq_to_guest_map[vector]) {
521 v3_deliver_irq(irq_to_guest_map[vector], &intr);
527 * Instructs the kernel to forward the specified IRQ to Palacios.
530 palacios_hook_interrupt(struct v3_vm_info * vm,
531 unsigned int vector ) {
532 INFO("hooking vector %d\n", vector);
534 if (irq_to_guest_map[vector]) {
536 "%s: Interrupt vector %u is already hooked.\n",
542 "%s: Hooking interrupt vector %u to vm %p.\n",
543 __func__, vector, vm);
545 irq_to_guest_map[vector] = vm;
548 * NOTE: Normally PCI devices are supposed to be level sensitive,
549 * but we need them to be edge sensitive so that they are
550 * properly latched by Palacios. Leaving them as level
551 * sensitive would lead to an interrupt storm.
553 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
555 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
557 ERROR("unexpected vector for hooking\n");
565 DEBUG("hooking vector: %d\n", vector);
573 error = request_irq((vector - 32),
574 (void *)palacios_dispatch_interrupt,
576 "interrupt_for_palacios",
580 ERROR("error code for request_irq is %d\n", error);
581 ERROR("request vector %d failed", vector);
592 * Acknowledges an interrupt.
595 palacios_ack_interrupt(
600 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
605 * Returns the CPU frequency in kilohertz.
608 palacios_get_cpu_khz(void)
610 INFO("cpu_khz is %u\n", cpu_khz);
613 INFO("faking cpu_khz to 1000000\n");
622 * Yield the CPU so other host OS tasks can run.
623 * This will return immediately if there is no other thread that is runnable
624 * And there is no real bound on how long it will yield
627 palacios_yield_cpu(void)
634 * Yield the CPU so other host OS tasks can run.
635 * Given now immediately if there is no other thread that is runnable
636 * And there is no real bound on how long it will yield
638 void palacios_sleep_cpu(unsigned int us)
641 set_current_state(TASK_INTERRUPTIBLE);
643 unsigned int uspj = 1000000U/HZ;
644 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
645 schedule_timeout(jiffies);
652 void palacios_wakeup_cpu(void *thread)
654 wake_up_process(thread);
660 * Returns NULL on failure.
663 palacios_mutex_alloc(void)
665 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
668 spin_lock_init(lock);
669 LOCKCHECK_ALLOC(lock);
671 ERROR("ALERT ALERT Unable to allocate lock\n");
678 void palacios_mutex_init(void *mutex)
680 spinlock_t *lock = (spinlock_t*)mutex;
683 spin_lock_init(lock);
684 LOCKCHECK_ALLOC(lock);
688 void palacios_mutex_deinit(void *mutex)
690 spinlock_t *lock = (spinlock_t*)mutex;
693 // no actual spin_lock_deinit on linux
694 // our purpose here is to drive the lock checker
695 LOCKCHECK_FREE(lock);
704 palacios_mutex_free(void * mutex) {
705 palacios_free(mutex);
706 LOCKCHECK_FREE(mutex);
713 palacios_mutex_lock(void * mutex, int must_spin) {
715 LOCKCHECK_LOCK_PRE(mutex);
716 spin_lock((spinlock_t *)mutex);
717 LOCKCHECK_LOCK_POST(mutex);
722 * Locks a mutex, disabling interrupts on this core
725 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
729 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
730 spin_lock_irqsave((spinlock_t *)mutex,flags);
731 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
733 return (void *)flags;
741 palacios_mutex_unlock(
745 LOCKCHECK_UNLOCK_PRE(mutex);
746 spin_unlock((spinlock_t *)mutex);
747 LOCKCHECK_UNLOCK_POST(mutex);
752 * Unlocks a mutex and restores previous interrupt state on this core
755 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
757 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
758 // This is correct, flags is opaque
759 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
760 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
764 * Structure used by the Palacios hypervisor to interface with the host kernel.
766 static struct v3_os_hooks palacios_os_hooks = {
767 .print = palacios_print_scoped,
768 .allocate_pages = palacios_allocate_pages,
769 .free_pages = palacios_free_pages,
770 .malloc = palacios_alloc,
771 .free = palacios_free,
772 .vaddr_to_paddr = palacios_vaddr_to_paddr,
773 .paddr_to_vaddr = palacios_paddr_to_vaddr,
774 .hook_interrupt = palacios_hook_interrupt,
775 .ack_irq = palacios_ack_interrupt,
776 .get_cpu_khz = palacios_get_cpu_khz,
777 .start_kernel_thread = palacios_start_kernel_thread,
778 .yield_cpu = palacios_yield_cpu,
779 .sleep_cpu = palacios_sleep_cpu,
780 .wakeup_cpu = palacios_wakeup_cpu,
781 .mutex_alloc = palacios_mutex_alloc,
782 .mutex_free = palacios_mutex_free,
783 .mutex_lock = palacios_mutex_lock,
784 .mutex_unlock = palacios_mutex_unlock,
785 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
786 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
787 .get_cpu = palacios_get_cpu,
788 .interrupt_cpu = palacios_interrupt_cpu,
789 .call_on_cpu = palacios_xcall,
790 .start_thread_on_cpu = palacios_start_thread_on_cpu,
791 .move_thread_to_cpu = palacios_move_thread_to_cpu,
797 int palacios_vmm_init( char *options )
799 int num_cpus = num_online_cpus();
800 char * cpu_mask = NULL;
802 if (cpu_list_len > 0) {
807 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
810 ERROR("Cannot allocate cpu mask\n");
814 memset(cpu_mask, 0, (num_cpus / 8) + 1);
816 for (i = 0; i < cpu_list_len; i++) {
817 if (cpu_list[i] >= num_cpus) {
818 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
822 major = cpu_list[i] / 8;
823 minor = cpu_list[i] % 8;
825 *(cpu_mask + major) |= (0x1 << minor);
829 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
831 if (init_print_buffers()) {
832 ERROR("Cannot initialize print buffers\n");
833 palacios_free(cpu_mask);
837 INFO("palacios_init starting - calling init_v3\n");
839 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
846 int palacios_vmm_exit( void ) {
850 INFO("palacios shutdown complete\n");
852 deinit_print_buffers();