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>
18 #include <palacios/vmm.h>
19 #include <palacios/vmm_host_events.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
40 static struct v3_vm_info * irq_to_guest_map[256];
43 extern unsigned int cpu_khz;
45 extern int cpu_list[NR_CPUS];
46 extern int cpu_list_len;
49 static char *print_buffer[NR_CPUS];
51 static void deinit_print_buffers(void)
55 for (i=0;i<NR_CPUS;i++) {
56 if (print_buffer[i]) {
57 palacios_free(print_buffer[i]);
63 static int init_print_buffers(void)
67 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
69 #if !V3_PRINTK_OLD_STYLE_OUTPUT
71 for (i=0;i<NR_CPUS;i++) {
72 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
73 if (!print_buffer[i]) {
74 ERROR("Cannot allocate print buffer for cpu %d\n",i);
75 deinit_print_buffers();
78 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
88 * Prints a message to the console.
90 void palacios_print(const char *fmt, ...) {
92 #if V3_PRINTK_OLD_STYLE_OUTPUT
106 unsigned int cpu = palacios_get_cpu();
108 buf = print_buffer[cpu];
111 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
116 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
119 #if V3_PRINTK_CHECK_7BIT
123 for (i=0;i<strlen(buf);i++) {
130 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
135 printk(KERN_INFO "palacios (pcore %u): %s",cpu,buf);
146 * Allocates a contiguous region of pages of the requested size.
147 * Returns the physical address of the first page in the region.
149 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
150 void * pg_addr = NULL;
152 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
155 ERROR("ALERT ALERT Page allocation has FAILED Warning\n");
159 pg_allocs += num_pages;
166 * Frees a page previously allocated via palacios_allocate_page().
167 * Note that palacios_allocate_page() can allocate multiple pages with
168 * a single call while palacios_free_page() only frees a single page.
171 void palacios_free_pages(void * page_paddr, int num_pages) {
172 pg_frees += num_pages;
173 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
178 palacios_alloc_extended(unsigned int size, unsigned int flags) {
181 addr = kmalloc(size+2*ALLOC_PAD, flags);
184 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
191 memset(addr,0,size+2*ALLOC_PAD);
194 return addr+ALLOC_PAD;
199 * Allocates 'size' bytes of kernel memory.
200 * Returns the kernel virtual address of the memory allocated.
203 palacios_alloc(unsigned int size) {
205 // It is very important that this test remains since
206 // this function is used extensively throughout palacios and the linux
207 // module, both in places where interrupts are off and where they are on
208 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
209 if (irqs_disabled()) {
210 return palacios_alloc_extended(size,GFP_ATOMIC);
212 return palacios_alloc_extended(size,GFP_KERNEL);
218 * Frees memory that was previously allocated by palacios_alloc().
226 kfree(addr-ALLOC_PAD);
231 * Converts a kernel virtual address to the corresponding physical address.
234 palacios_vaddr_to_paddr(
238 return (void*) __pa(vaddr);
243 * Converts a physical address to the corresponding kernel virtual address.
246 palacios_paddr_to_vaddr(
254 * Runs a function on the specified CPU.
259 void (*fn)(void *arg),
265 // We set wait to 1, but I'm not sure this is necessary
266 smp_call_function_single(cpu_id, fn, arg, 1);
271 struct lnx_thread_arg {
272 int (*fn)(void * arg);
277 static int lnx_thread_target(void * arg) {
278 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
281 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
283 daemonize(thread_info->name);
284 allow_signal(SIGKILL);
288 ret = thread_info->fn(thread_info->arg);
291 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
293 palacios_free(thread_info);
298 return 0; // should not get here.
302 * Creates a kernel thread.
305 palacios_start_kernel_thread(
306 int (*fn) (void * arg),
308 char * thread_name) {
310 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
313 ERROR("ALERT ALERT Unable to allocate thread\n");
317 thread_info->fn = fn;
318 thread_info->arg = arg;
319 thread_info->name = thread_name;
321 return kthread_run( lnx_thread_target, thread_info, thread_name );
326 * Starts a kernel thread on the specified CPU.
329 palacios_start_thread_on_cpu(int cpu_id,
330 int (*fn)(void * arg),
332 char * thread_name ) {
333 struct task_struct * thread = NULL;
334 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
337 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
341 thread_info->fn = fn;
342 thread_info->arg = arg;
343 thread_info->name = thread_name;
346 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
348 if (IS_ERR(thread)) {
349 WARNING("Palacios error creating thread: %s\n", thread_name);
350 palacios_free(thread_info);
354 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
355 WARNING("Attempt to start thread on disallowed CPU\n");
356 kthread_stop(thread);
357 palacios_free(thread_info);
361 wake_up_process(thread);
368 * Rebind a kernel thread to the specified CPU
369 * The thread will be running on target CPU on return
370 * non-zero return means failure
373 palacios_move_thread_to_cpu(int new_cpu_id,
375 struct task_struct * thread = (struct task_struct *)thread_ptr;
377 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
379 if (thread == NULL) {
384 * Bind to the specified CPU. When this call returns,
385 * the thread should be running on the target CPU.
387 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
392 * Returns the CPU ID that the caller is running on.
395 palacios_get_cpu(void)
398 /* We want to call smp_processor_id()
399 * But this is not safe if kernel preemption is possible
400 * We need to ensure that the palacios threads are bound to a give cpu
403 unsigned int cpu_id = get_cpu();
409 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
412 * This is dependent on the implementation of xcall_reschedule(). Currently
413 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
414 * but instead relies on the return to user space to handle it. Because
415 * palacios is a kernel thread schedule will not be called, which is correct.
416 * If it ever changes to induce side effects, we'll need to figure something
420 #include <asm/apic.h>
423 palacios_interrupt_cpu(
424 struct v3_vm_info * vm,
430 smp_send_reschedule(cpu_id);
432 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
438 * Dispatches an interrupt to Palacios for handling.
441 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
442 struct v3_interrupt intr = {
444 .error = regs->orig_ax,
448 if (irq_to_guest_map[vector]) {
449 v3_deliver_irq(irq_to_guest_map[vector], &intr);
455 * Instructs the kernel to forward the specified IRQ to Palacios.
458 palacios_hook_interrupt(struct v3_vm_info * vm,
459 unsigned int vector ) {
460 INFO("hooking vector %d\n", vector);
462 if (irq_to_guest_map[vector]) {
464 "%s: Interrupt vector %u is already hooked.\n",
470 "%s: Hooking interrupt vector %u to vm %p.\n",
471 __func__, vector, vm);
473 irq_to_guest_map[vector] = vm;
476 * NOTE: Normally PCI devices are supposed to be level sensitive,
477 * but we need them to be edge sensitive so that they are
478 * properly latched by Palacios. Leaving them as level
479 * sensitive would lead to an interrupt storm.
481 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
483 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
485 ERROR("unexpected vector for hooking\n");
493 DEBUG("hooking vector: %d\n", vector);
501 error = request_irq((vector - 32),
502 (void *)palacios_dispatch_interrupt,
504 "interrupt_for_palacios",
508 ERROR("error code for request_irq is %d\n", error);
509 ERROR("request vector %d failed", vector);
520 * Acknowledges an interrupt.
523 palacios_ack_interrupt(
528 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
533 * Returns the CPU frequency in kilohertz.
536 palacios_get_cpu_khz(void)
538 INFO("cpu_khz is %u\n", cpu_khz);
541 INFO("faking cpu_khz to 1000000\n");
550 * Yield the CPU so other host OS tasks can run.
551 * This will return immediately if there is no other thread that is runnable
552 * And there is no real bound on how long it will yield
555 palacios_yield_cpu(void)
562 * Yield the CPU so other host OS tasks can run.
563 * Given now immediately if there is no other thread that is runnable
564 * And there is no real bound on how long it will yield
566 void palacios_yield_cpu_timed(unsigned int us)
569 unsigned int uspj = 1000000U/HZ;
571 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
573 set_current_state(TASK_INTERRUPTIBLE);
575 schedule_timeout(jiffies);
582 * Returns NULL on failure.
585 palacios_mutex_alloc(void)
587 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
590 spin_lock_init(lock);
592 ERROR("ALERT ALERT Unable to allocate lock\n");
603 palacios_mutex_free(void * mutex) {
604 palacios_free(mutex);
611 palacios_mutex_lock(void * mutex, int must_spin) {
612 spin_lock((spinlock_t *)mutex);
617 * Locks a mutex, disabling interrupts on this core
620 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
624 spin_lock_irqsave((spinlock_t *)mutex,flags);
626 return (void *)flags;
634 palacios_mutex_unlock(
638 spin_unlock((spinlock_t *)mutex);
643 * Unlocks a mutex and restores previous interrupt state on this core
646 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
648 // This is correct, flags is opaque
649 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
653 * Structure used by the Palacios hypervisor to interface with the host kernel.
655 static struct v3_os_hooks palacios_os_hooks = {
656 .print = palacios_print,
657 .allocate_pages = palacios_allocate_pages,
658 .free_pages = palacios_free_pages,
659 .malloc = palacios_alloc,
660 .free = palacios_free,
661 .vaddr_to_paddr = palacios_vaddr_to_paddr,
662 .paddr_to_vaddr = palacios_paddr_to_vaddr,
663 .hook_interrupt = palacios_hook_interrupt,
664 .ack_irq = palacios_ack_interrupt,
665 .get_cpu_khz = palacios_get_cpu_khz,
666 .start_kernel_thread = palacios_start_kernel_thread,
667 .yield_cpu = palacios_yield_cpu,
668 .yield_cpu_timed = palacios_yield_cpu_timed,
669 .mutex_alloc = palacios_mutex_alloc,
670 .mutex_free = palacios_mutex_free,
671 .mutex_lock = palacios_mutex_lock,
672 .mutex_unlock = palacios_mutex_unlock,
673 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
674 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
675 .get_cpu = palacios_get_cpu,
676 .interrupt_cpu = palacios_interrupt_cpu,
677 .call_on_cpu = palacios_xcall,
678 .start_thread_on_cpu = palacios_start_thread_on_cpu,
679 .move_thread_to_cpu = palacios_move_thread_to_cpu,
685 int palacios_vmm_init( void )
687 int num_cpus = num_online_cpus();
688 char * cpu_mask = NULL;
690 if (cpu_list_len > 0) {
695 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
698 ERROR("Cannot allocate cpu mask\n");
702 memset(cpu_mask, 0, (num_cpus / 8) + 1);
704 for (i = 0; i < cpu_list_len; i++) {
705 if (cpu_list[i] >= num_cpus) {
706 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
710 major = cpu_list[i] / 8;
711 minor = cpu_list[i] % 8;
713 *(cpu_mask + major) |= (0x1 << minor);
717 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
719 if (init_print_buffers()) {
720 ERROR("Cannot initialize print buffers\n");
721 palacios_free(cpu_mask);
725 INFO("palacios_init starting - calling init_v3\n");
727 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus);
734 int palacios_vmm_exit( void ) {
738 INFO("palacios shutdown complete\n");
740 deinit_print_buffers();