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, int node_id, int constraints) {
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, node_id, constraints);
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, int node) {
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");
218 addr = kmalloc(size+2*ALLOC_PAD, flags);
220 addr = kmalloc_node(size+2*ALLOC_PAD, flags, node);
224 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
231 memset(addr,0,size+2*ALLOC_PAD);
234 MEMCHECK_KMALLOC(addr,size+2*ALLOC_PAD);
236 return addr+ALLOC_PAD;
240 palacios_valloc(unsigned int size)
245 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
249 addr = vmalloc(size);
252 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
258 MEMCHECK_VMALLOC(addr,size);
263 void palacios_vfree(void *p)
271 * Allocates 'size' bytes of kernel memory.
272 * Returns the kernel virtual address of the memory allocated.
275 palacios_alloc(unsigned int size) {
277 // It is very important that this test remains since
278 // this function is used extensively throughout palacios and the linux
279 // module, both in places where interrupts are off and where they are on
280 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
281 if (irqs_disabled()) {
282 return palacios_alloc_extended(size,GFP_ATOMIC,-1);
284 return palacios_alloc_extended(size,GFP_KERNEL,-1);
290 * Frees memory that was previously allocated by palacios_alloc().
298 kfree(addr-ALLOC_PAD);
299 MEMCHECK_KFREE(addr-ALLOC_PAD);
303 * Converts a kernel virtual address to the corresponding physical address.
306 palacios_vaddr_to_paddr(
310 return (void*) __pa(vaddr);
315 * Converts a physical address to the corresponding kernel virtual address.
318 palacios_paddr_to_vaddr(
326 * Runs a function on the specified CPU.
331 void (*fn)(void *arg),
337 // We set wait to 1, but I'm not sure this is necessary
338 smp_call_function_single(cpu_id, fn, arg, 1);
344 #define MAX_THREAD_NAME 32
346 struct lnx_thread_arg {
347 int (*fn)(void * arg);
349 char name[MAX_THREAD_NAME];
352 static int lnx_thread_target(void * arg) {
353 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
356 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
358 daemonize(thread_info->name);
359 allow_signal(SIGKILL);
363 ret = thread_info->fn(thread_info->arg);
366 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
368 palacios_free(thread_info);
373 return 0; // should not get here.
377 * Creates a kernel thread.
380 palacios_start_kernel_thread(
381 int (*fn) (void * arg),
383 char * thread_name) {
385 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
388 ERROR("ALERT ALERT Unable to allocate thread\n");
392 thread_info->fn = fn;
393 thread_info->arg = arg;
394 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
395 thread_info->name[MAX_THREAD_NAME-1] =0;
397 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
402 * Starts a kernel thread on the specified CPU.
405 palacios_start_thread_on_cpu(int cpu_id,
406 int (*fn)(void * arg),
408 char * thread_name ) {
409 struct task_struct * thread = NULL;
410 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
413 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
417 thread_info->fn = fn;
418 thread_info->arg = arg;
419 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
420 thread_info->name[MAX_THREAD_NAME-1] =0;
422 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
424 if (IS_ERR(thread)) {
425 WARNING("Palacios error creating thread: %s\n", thread_info->name);
426 palacios_free(thread_info);
430 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
431 WARNING("Attempt to start thread on disallowed CPU\n");
432 kthread_stop(thread);
433 palacios_free(thread_info);
437 wake_up_process(thread);
444 * Rebind a kernel thread to the specified CPU
445 * The thread will be running on target CPU on return
446 * non-zero return means failure
449 palacios_move_thread_to_cpu(int new_cpu_id,
451 struct task_struct * thread = (struct task_struct *)thread_ptr;
453 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
455 if (thread == NULL) {
460 * Bind to the specified CPU. When this call returns,
461 * the thread should be running on the target CPU.
463 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
468 * Returns the CPU ID that the caller is running on.
471 palacios_get_cpu(void)
474 /* We want to call smp_processor_id()
475 * But this is not safe if kernel preemption is possible
476 * We need to ensure that the palacios threads are bound to a give cpu
479 unsigned int cpu_id = get_cpu();
485 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
488 * This is dependent on the implementation of xcall_reschedule(). Currently
489 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
490 * but instead relies on the return to user space to handle it. Because
491 * palacios is a kernel thread schedule will not be called, which is correct.
492 * If it ever changes to induce side effects, we'll need to figure something
496 #include <asm/apic.h>
499 palacios_interrupt_cpu(
500 struct v3_vm_info * vm,
506 smp_send_reschedule(cpu_id);
508 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
514 * Dispatches an interrupt to Palacios for handling.
517 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
518 struct v3_interrupt intr = {
520 .error = regs->orig_ax,
524 if (irq_to_guest_map[vector]) {
525 v3_deliver_irq(irq_to_guest_map[vector], &intr);
531 * Instructs the kernel to forward the specified IRQ to Palacios.
534 palacios_hook_interrupt(struct v3_vm_info * vm,
535 unsigned int vector ) {
536 INFO("hooking vector %d\n", vector);
538 if (irq_to_guest_map[vector]) {
540 "%s: Interrupt vector %u is already hooked.\n",
546 "%s: Hooking interrupt vector %u to vm %p.\n",
547 __func__, vector, vm);
549 irq_to_guest_map[vector] = vm;
552 * NOTE: Normally PCI devices are supposed to be level sensitive,
553 * but we need them to be edge sensitive so that they are
554 * properly latched by Palacios. Leaving them as level
555 * sensitive would lead to an interrupt storm.
557 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
559 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
561 ERROR("unexpected vector for hooking\n");
569 DEBUG("hooking vector: %d\n", vector);
577 error = request_irq((vector - 32),
578 (void *)palacios_dispatch_interrupt,
580 "interrupt_for_palacios",
584 ERROR("error code for request_irq is %d\n", error);
585 ERROR("request vector %d failed", vector);
596 * Acknowledges an interrupt.
599 palacios_ack_interrupt(
604 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
609 * Returns the CPU frequency in kilohertz.
612 palacios_get_cpu_khz(void)
614 INFO("cpu_khz is %u\n", cpu_khz);
617 INFO("faking cpu_khz to 1000000\n");
626 * Yield the CPU so other host OS tasks can run.
627 * This will return immediately if there is no other thread that is runnable
628 * And there is no real bound on how long it will yield
631 palacios_yield_cpu(void)
638 * Yield the CPU so other host OS tasks can run.
639 * Given now immediately if there is no other thread that is runnable
640 * And there is no real bound on how long it will yield
642 void palacios_sleep_cpu(unsigned int us)
645 set_current_state(TASK_INTERRUPTIBLE);
647 unsigned int uspj = 1000000U/HZ;
648 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
649 schedule_timeout(jiffies);
656 void palacios_wakeup_cpu(void *thread)
658 wake_up_process(thread);
664 * Returns NULL on failure.
667 palacios_mutex_alloc(void)
669 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
672 spin_lock_init(lock);
673 LOCKCHECK_ALLOC(lock);
675 ERROR("ALERT ALERT Unable to allocate lock\n");
682 void palacios_mutex_init(void *mutex)
684 spinlock_t *lock = (spinlock_t*)mutex;
687 spin_lock_init(lock);
688 LOCKCHECK_ALLOC(lock);
692 void palacios_mutex_deinit(void *mutex)
694 spinlock_t *lock = (spinlock_t*)mutex;
697 // no actual spin_lock_deinit on linux
698 // our purpose here is to drive the lock checker
699 LOCKCHECK_FREE(lock);
708 palacios_mutex_free(void * mutex) {
709 palacios_free(mutex);
710 LOCKCHECK_FREE(mutex);
717 palacios_mutex_lock(void * mutex, int must_spin) {
719 LOCKCHECK_LOCK_PRE(mutex);
720 spin_lock((spinlock_t *)mutex);
721 LOCKCHECK_LOCK_POST(mutex);
726 * Locks a mutex, disabling interrupts on this core
729 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
733 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
734 spin_lock_irqsave((spinlock_t *)mutex,flags);
735 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
737 return (void *)flags;
745 palacios_mutex_unlock(
749 LOCKCHECK_UNLOCK_PRE(mutex);
750 spin_unlock((spinlock_t *)mutex);
751 LOCKCHECK_UNLOCK_POST(mutex);
756 * Unlocks a mutex and restores previous interrupt state on this core
759 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
761 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
762 // This is correct, flags is opaque
763 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
764 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
768 * Structure used by the Palacios hypervisor to interface with the host kernel.
770 static struct v3_os_hooks palacios_os_hooks = {
771 .print = palacios_print_scoped,
772 .allocate_pages = palacios_allocate_pages,
773 .free_pages = palacios_free_pages,
774 .malloc = palacios_alloc,
775 .free = palacios_free,
776 .vaddr_to_paddr = palacios_vaddr_to_paddr,
777 .paddr_to_vaddr = palacios_paddr_to_vaddr,
778 .hook_interrupt = palacios_hook_interrupt,
779 .ack_irq = palacios_ack_interrupt,
780 .get_cpu_khz = palacios_get_cpu_khz,
781 .start_kernel_thread = palacios_start_kernel_thread,
782 .yield_cpu = palacios_yield_cpu,
783 .sleep_cpu = palacios_sleep_cpu,
784 .wakeup_cpu = palacios_wakeup_cpu,
785 .mutex_alloc = palacios_mutex_alloc,
786 .mutex_free = palacios_mutex_free,
787 .mutex_lock = palacios_mutex_lock,
788 .mutex_unlock = palacios_mutex_unlock,
789 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
790 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
791 .get_cpu = palacios_get_cpu,
792 .interrupt_cpu = palacios_interrupt_cpu,
793 .call_on_cpu = palacios_xcall,
794 .start_thread_on_cpu = palacios_start_thread_on_cpu,
795 .move_thread_to_cpu = palacios_move_thread_to_cpu,
801 int palacios_vmm_init( char *options )
803 int num_cpus = num_online_cpus();
804 char * cpu_mask = NULL;
806 if (cpu_list_len > 0) {
811 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
814 ERROR("Cannot allocate cpu mask\n");
818 memset(cpu_mask, 0, (num_cpus / 8) + 1);
820 for (i = 0; i < cpu_list_len; i++) {
821 if (cpu_list[i] >= num_cpus) {
822 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
826 major = cpu_list[i] / 8;
827 minor = cpu_list[i] % 8;
829 *(cpu_mask + major) |= (0x1 << minor);
833 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
835 if (init_print_buffers()) {
836 ERROR("Cannot initialize print buffers\n");
837 palacios_free(cpu_mask);
841 INFO("palacios_init starting - calling init_v3\n");
843 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
850 int palacios_vmm_exit( void ) {
854 INFO("palacios shutdown complete\n");
856 deinit_print_buffers();