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;
46 * Prints a message to the console.
48 static void palacios_print(const char * fmt, ...) {
50 #if V3_PRINTK_OLD_STYLE_OUTPUT
65 // Allocate space atomically, in case we are called
67 buf = kmalloc(V3_PRINTK_BUF_SIZE, GFP_ATOMIC);
69 printk("palacios: output skipped - unable to allocate\n");
74 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
77 printk(KERN_INFO "palacios: %s",buf);
90 * Allocates a contiguous region of pages of the requested size.
91 * Returns the physical address of the first page in the region.
93 static void * palacios_allocate_pages(int num_pages, unsigned int alignment) {
94 void * pg_addr = NULL;
96 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
97 pg_allocs += num_pages;
104 * Frees a page previously allocated via palacios_allocate_page().
105 * Note that palacios_allocate_page() can allocate multiple pages with
106 * a single call while palacios_free_page() only frees a single page.
109 static void palacios_free_pages(void * page_paddr, int num_pages) {
110 pg_frees += num_pages;
111 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
116 * Allocates 'size' bytes of kernel memory.
117 * Returns the kernel virtual address of the memory allocated.
120 palacios_alloc(unsigned int size) {
123 if (irqs_disabled()) {
124 addr = kmalloc(size, GFP_ATOMIC);
126 addr = kmalloc(size, GFP_KERNEL);
135 * Frees memory that was previously allocated by palacios_alloc().
148 * Converts a kernel virtual address to the corresponding physical address.
151 palacios_vaddr_to_paddr(
155 return (void*) __pa(vaddr);
160 * Converts a physical address to the corresponding kernel virtual address.
163 palacios_paddr_to_vaddr(
171 * Runs a function on the specified CPU.
174 // For now, do call only on local CPU
178 void (*fn)(void *arg),
184 // We set wait to 1, but I'm not sure this is necessary
185 smp_call_function_single(cpu_id, fn, arg, 1);
190 struct lnx_thread_arg {
191 int (*fn)(void * arg);
196 static int lnx_thread_target(void * arg) {
197 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
200 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
202 daemonize(thread_info->name);
203 allow_signal(SIGKILL);
207 ret = thread_info->fn(thread_info->arg);
213 printk("Palacios Thread (%s) EXITTING\n", thread_info->name);
217 return 0; // should not get here.
221 * Creates a kernel thread.
224 palacios_start_kernel_thread(
225 int (*fn) (void * arg),
227 char * thread_name) {
229 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
231 thread_info->fn = fn;
232 thread_info->arg = arg;
233 thread_info->name = thread_name;
235 return kthread_run( lnx_thread_target, thread_info, thread_name );
240 * Starts a kernel thread on the specified CPU.
243 palacios_start_thread_on_cpu(int cpu_id,
244 int (*fn)(void * arg),
246 char * thread_name ) {
247 struct task_struct * thread = NULL;
248 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
250 thread_info->fn = fn;
251 thread_info->arg = arg;
252 thread_info->name = thread_name;
255 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
257 if (IS_ERR(thread)) {
258 WARNING("Palacios error creating thread: %s\n", thread_name);
262 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
263 kthread_stop(thread);
267 wake_up_process(thread);
274 * Rebind a kernel thread to the specified CPU
275 * The thread will be running on target CPU on return
276 * non-zero return means failure
279 palacios_move_thread_to_cpu(int new_cpu_id,
281 struct task_struct * thread = (struct task_struct *)thread_ptr;
283 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
285 if (thread == NULL) {
290 * Bind to the specified CPU. When this call returns,
291 * the thread should be running on the target CPU.
293 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
298 * Returns the CPU ID that the caller is running on.
301 palacios_get_cpu(void)
304 /* We want to call smp_processor_id()
305 * But this is not safe if kernel preemption is possible
306 * We need to ensure that the palacios threads are bound to a give cpu
309 unsigned int cpu_id = get_cpu();
315 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
318 * This is dependent on the implementation of xcall_reschedule(). Currently
319 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
320 * but instead relies on the return to user space to handle it. Because
321 * palacios is a kernel thread schedule will not be called, which is correct.
322 * If it ever changes to induce side effects, we'll need to figure something
326 #include <asm/apic.h>
329 palacios_interrupt_cpu(
330 struct v3_vm_info * vm,
336 smp_send_reschedule(cpu_id);
338 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
344 * Dispatches an interrupt to Palacios for handling.
347 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
348 struct v3_interrupt intr = {
350 .error = regs->orig_ax,
354 if (irq_to_guest_map[vector]) {
355 v3_deliver_irq(irq_to_guest_map[vector], &intr);
361 * Instructs the kernel to forward the specified IRQ to Palacios.
364 palacios_hook_interrupt(struct v3_vm_info * vm,
365 unsigned int vector ) {
366 INFO("hooking vector %d\n", vector);
368 if (irq_to_guest_map[vector]) {
370 "%s: Interrupt vector %u is already hooked.\n",
376 "%s: Hooking interrupt vector %u to vm %p.\n",
377 __func__, vector, vm);
379 irq_to_guest_map[vector] = vm;
382 * NOTE: Normally PCI devices are supposed to be level sensitive,
383 * but we need them to be edge sensitive so that they are
384 * properly latched by Palacios. Leaving them as level
385 * sensitive would lead to an interrupt storm.
387 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
389 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
391 panic("unexpected vector for hooking\n");
398 DEBUG("hooking vector: %d\n", vector);
406 error = request_irq((vector - 32),
407 (void *)palacios_dispatch_interrupt,
409 "interrupt_for_palacios",
413 ERROR("error code for request_irq is %d\n", error);
414 panic("request vector %d failed",vector);
424 * Acknowledges an interrupt.
427 palacios_ack_interrupt(
432 DEBUG("Pretending to ack interrupt, vector=%d\n",vector);
437 * Returns the CPU frequency in kilohertz.
440 palacios_get_cpu_khz(void)
442 INFO("cpu_khz is %u\n",cpu_khz);
445 INFO("faking cpu_khz to 1000000\n");
454 * Yield the CPU so other host OS tasks can run.
457 palacios_yield_cpu(void)
467 * Returns NULL on failure.
470 palacios_mutex_alloc(void)
472 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
475 spin_lock_init(lock);
485 palacios_mutex_free(void * mutex) {
493 palacios_mutex_lock(void * mutex, int must_spin) {
494 spin_lock((spinlock_t *)mutex);
501 palacios_mutex_unlock(
505 spin_unlock((spinlock_t *)mutex);
509 * Structure used by the Palacios hypervisor to interface with the host kernel.
511 static struct v3_os_hooks palacios_os_hooks = {
512 .print = palacios_print,
513 .allocate_pages = palacios_allocate_pages,
514 .free_pages = palacios_free_pages,
515 .malloc = palacios_alloc,
516 .free = palacios_free,
517 .vaddr_to_paddr = palacios_vaddr_to_paddr,
518 .paddr_to_vaddr = palacios_paddr_to_vaddr,
519 .hook_interrupt = palacios_hook_interrupt,
520 .ack_irq = palacios_ack_interrupt,
521 .get_cpu_khz = palacios_get_cpu_khz,
522 .start_kernel_thread = palacios_start_kernel_thread,
523 .yield_cpu = palacios_yield_cpu,
524 .mutex_alloc = palacios_mutex_alloc,
525 .mutex_free = palacios_mutex_free,
526 .mutex_lock = palacios_mutex_lock,
527 .mutex_unlock = palacios_mutex_unlock,
528 .get_cpu = palacios_get_cpu,
529 .interrupt_cpu = palacios_interrupt_cpu,
530 .call_on_cpu = palacios_xcall,
531 .start_thread_on_cpu = palacios_start_thread_on_cpu,
532 .move_thread_to_cpu = palacios_move_thread_to_cpu,
538 int palacios_vmm_init( void )
540 int num_cpus = num_online_cpus();
541 char * cpu_mask = NULL;
543 if (cpu_list_len > 0) {
548 cpu_mask = kmalloc((num_cpus / 8) + 1, GFP_KERNEL);
549 memset(cpu_mask, 0, (num_cpus / 8) + 1);
551 for (i = 0; i < cpu_list_len; i++) {
552 if (cpu_list[i] >= num_cpus) {
553 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
557 major = cpu_list[i] / 8;
558 minor = cpu_list[i] % 8;
560 *(cpu_mask + major) |= (0x1 << minor);
564 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
566 INFO("palacios_init starting - calling init_v3\n");
568 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus);
575 int palacios_vmm_exit( void ) {