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>
34 static struct v3_vm_info * irq_to_guest_map[256];
37 extern unsigned int cpu_khz;
39 extern int cpu_list[NR_CPUS];
40 extern int cpu_list_len;
43 static char *print_buffer[NR_CPUS];
45 static void deinit_print_buffers(void)
49 for (i=0;i<NR_CPUS;i++) {
50 if (print_buffer[i]) {
51 palacios_free(print_buffer[i]);
57 static int init_print_buffers(void)
61 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
63 #if !V3_PRINTK_OLD_STYLE_OUTPUT
65 for (i=0;i<NR_CPUS;i++) {
66 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
67 if (!print_buffer[i]) {
68 ERROR("Cannot allocate print buffer for cpu %d\n",i);
69 deinit_print_buffers();
72 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
82 * Prints a message to the console.
84 void palacios_print(const char *fmt, ...) {
86 #if V3_PRINTK_OLD_STYLE_OUTPUT
100 unsigned int cpu = palacios_get_cpu();
102 buf = print_buffer[cpu];
105 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
110 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
113 #if V3_PRINTK_CHECK_7BIT
117 for (i=0;i<strlen(buf);i++) {
124 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
129 printk(KERN_INFO "palacios (pcore %u): %s",cpu,buf);
140 * Allocates a contiguous region of pages of the requested size.
141 * Returns the physical address of the first page in the region.
143 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
144 void * pg_addr = NULL;
146 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
149 ERROR("ALERT ALERT Page allocation has FAILED Warning\n");
153 pg_allocs += num_pages;
160 * Frees a page previously allocated via palacios_allocate_page().
161 * Note that palacios_allocate_page() can allocate multiple pages with
162 * a single call while palacios_free_page() only frees a single page.
165 void palacios_free_pages(void * page_paddr, int num_pages) {
166 pg_frees += num_pages;
167 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
172 * Allocates 'size' bytes of kernel memory.
173 * Returns the kernel virtual address of the memory allocated.
176 palacios_alloc(unsigned int size) {
179 // It is very important that this test remains since
180 // this function is used extensively throughout palacios and the linux
181 // module, both in places where interrupts are off and where they are on
182 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
183 if (irqs_disabled()) {
184 addr = kmalloc(size, GFP_ATOMIC);
186 addr = kmalloc(size, GFP_KERNEL);
190 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
200 * Frees memory that was previously allocated by palacios_alloc().
213 * Converts a kernel virtual address to the corresponding physical address.
216 palacios_vaddr_to_paddr(
220 return (void*) __pa(vaddr);
225 * Converts a physical address to the corresponding kernel virtual address.
228 palacios_paddr_to_vaddr(
236 * Runs a function on the specified CPU.
241 void (*fn)(void *arg),
247 // We set wait to 1, but I'm not sure this is necessary
248 smp_call_function_single(cpu_id, fn, arg, 1);
253 struct lnx_thread_arg {
254 int (*fn)(void * arg);
259 static int lnx_thread_target(void * arg) {
260 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
263 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
265 daemonize(thread_info->name);
266 allow_signal(SIGKILL);
270 ret = thread_info->fn(thread_info->arg);
273 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
275 palacios_free(thread_info);
280 return 0; // should not get here.
284 * Creates a kernel thread.
287 palacios_start_kernel_thread(
288 int (*fn) (void * arg),
290 char * thread_name) {
292 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
295 ERROR("ALERT ALERT Unable to allocate thread\n");
299 thread_info->fn = fn;
300 thread_info->arg = arg;
301 thread_info->name = thread_name;
303 return kthread_run( lnx_thread_target, thread_info, thread_name );
308 * Starts a kernel thread on the specified CPU.
311 palacios_start_thread_on_cpu(int cpu_id,
312 int (*fn)(void * arg),
314 char * thread_name ) {
315 struct task_struct * thread = NULL;
316 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
319 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
323 thread_info->fn = fn;
324 thread_info->arg = arg;
325 thread_info->name = thread_name;
328 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
330 if (IS_ERR(thread)) {
331 WARNING("Palacios error creating thread: %s\n", thread_name);
332 palacios_free(thread_info);
336 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
337 WARNING("Attempt to start thread on disallowed CPU\n");
338 kthread_stop(thread);
339 palacios_free(thread_info);
343 wake_up_process(thread);
350 * Rebind a kernel thread to the specified CPU
351 * The thread will be running on target CPU on return
352 * non-zero return means failure
355 palacios_move_thread_to_cpu(int new_cpu_id,
357 struct task_struct * thread = (struct task_struct *)thread_ptr;
359 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
361 if (thread == NULL) {
366 * Bind to the specified CPU. When this call returns,
367 * the thread should be running on the target CPU.
369 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
374 * Returns the CPU ID that the caller is running on.
377 palacios_get_cpu(void)
380 /* We want to call smp_processor_id()
381 * But this is not safe if kernel preemption is possible
382 * We need to ensure that the palacios threads are bound to a give cpu
385 unsigned int cpu_id = get_cpu();
391 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
394 * This is dependent on the implementation of xcall_reschedule(). Currently
395 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
396 * but instead relies on the return to user space to handle it. Because
397 * palacios is a kernel thread schedule will not be called, which is correct.
398 * If it ever changes to induce side effects, we'll need to figure something
402 #include <asm/apic.h>
405 palacios_interrupt_cpu(
406 struct v3_vm_info * vm,
412 smp_send_reschedule(cpu_id);
414 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
420 * Dispatches an interrupt to Palacios for handling.
423 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
424 struct v3_interrupt intr = {
426 .error = regs->orig_ax,
430 if (irq_to_guest_map[vector]) {
431 v3_deliver_irq(irq_to_guest_map[vector], &intr);
437 * Instructs the kernel to forward the specified IRQ to Palacios.
440 palacios_hook_interrupt(struct v3_vm_info * vm,
441 unsigned int vector ) {
442 INFO("hooking vector %d\n", vector);
444 if (irq_to_guest_map[vector]) {
446 "%s: Interrupt vector %u is already hooked.\n",
452 "%s: Hooking interrupt vector %u to vm %p.\n",
453 __func__, vector, vm);
455 irq_to_guest_map[vector] = vm;
458 * NOTE: Normally PCI devices are supposed to be level sensitive,
459 * but we need them to be edge sensitive so that they are
460 * properly latched by Palacios. Leaving them as level
461 * sensitive would lead to an interrupt storm.
463 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
465 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
467 ERROR("unexpected vector for hooking\n");
475 DEBUG("hooking vector: %d\n", vector);
483 error = request_irq((vector - 32),
484 (void *)palacios_dispatch_interrupt,
486 "interrupt_for_palacios",
490 ERROR("error code for request_irq is %d\n", error);
491 ERROR("request vector %d failed", vector);
502 * Acknowledges an interrupt.
505 palacios_ack_interrupt(
510 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
515 * Returns the CPU frequency in kilohertz.
518 palacios_get_cpu_khz(void)
520 INFO("cpu_khz is %u\n", cpu_khz);
523 INFO("faking cpu_khz to 1000000\n");
532 * Yield the CPU so other host OS tasks can run.
533 * This will return immediately if there is no other thread that is runnable
534 * And there is no real bound on how long it will yield
537 palacios_yield_cpu(void)
544 * Yield the CPU so other host OS tasks can run.
545 * Given now immediately if there is no other thread that is runnable
546 * And there is no real bound on how long it will yield
548 void palacios_yield_cpu_timed(unsigned int us)
551 unsigned int uspj = 1000000U/HZ;
553 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
555 set_current_state(TASK_INTERRUPTIBLE);
557 schedule_timeout(jiffies);
564 * Returns NULL on failure.
567 palacios_mutex_alloc(void)
569 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
572 spin_lock_init(lock);
574 ERROR("ALERT ALERT Unable to allocate lock\n");
585 palacios_mutex_free(void * mutex) {
586 palacios_free(mutex);
593 palacios_mutex_lock(void * mutex, int must_spin) {
594 spin_lock((spinlock_t *)mutex);
599 * Locks a mutex, disabling interrupts on this core
602 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
606 spin_lock_irqsave((spinlock_t *)mutex,flags);
608 return (void *)flags;
616 palacios_mutex_unlock(
620 spin_unlock((spinlock_t *)mutex);
625 * Unlocks a mutex and restores previous interrupt state on this core
628 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
630 // This is correct, flags is opaque
631 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
635 * Structure used by the Palacios hypervisor to interface with the host kernel.
637 static struct v3_os_hooks palacios_os_hooks = {
638 .print = palacios_print,
639 .allocate_pages = palacios_allocate_pages,
640 .free_pages = palacios_free_pages,
641 .malloc = palacios_alloc,
642 .free = palacios_free,
643 .vaddr_to_paddr = palacios_vaddr_to_paddr,
644 .paddr_to_vaddr = palacios_paddr_to_vaddr,
645 .hook_interrupt = palacios_hook_interrupt,
646 .ack_irq = palacios_ack_interrupt,
647 .get_cpu_khz = palacios_get_cpu_khz,
648 .start_kernel_thread = palacios_start_kernel_thread,
649 .yield_cpu = palacios_yield_cpu,
650 .yield_cpu_timed = palacios_yield_cpu_timed,
651 .mutex_alloc = palacios_mutex_alloc,
652 .mutex_free = palacios_mutex_free,
653 .mutex_lock = palacios_mutex_lock,
654 .mutex_unlock = palacios_mutex_unlock,
655 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
656 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
657 .get_cpu = palacios_get_cpu,
658 .interrupt_cpu = palacios_interrupt_cpu,
659 .call_on_cpu = palacios_xcall,
660 .start_thread_on_cpu = palacios_start_thread_on_cpu,
661 .move_thread_to_cpu = palacios_move_thread_to_cpu,
667 int palacios_vmm_init( void )
669 int num_cpus = num_online_cpus();
670 char * cpu_mask = NULL;
672 if (cpu_list_len > 0) {
677 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
680 ERROR("Cannot allocate cpu mask\n");
684 memset(cpu_mask, 0, (num_cpus / 8) + 1);
686 for (i = 0; i < cpu_list_len; i++) {
687 if (cpu_list[i] >= num_cpus) {
688 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
692 major = cpu_list[i] / 8;
693 minor = cpu_list[i] % 8;
695 *(cpu_mask + major) |= (0x1 << minor);
699 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
701 if (init_print_buffers()) {
702 ERROR("Cannot initialize print buffers\n");
703 palacios_free(cpu_mask);
707 INFO("palacios_init starting - calling init_v3\n");
709 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus);
716 int palacios_vmm_exit( void ) {
720 INFO("palacios shutdown complete\n");
722 deinit_print_buffers();