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;
41 * Prints a message to the console.
43 static void palacios_print(const char * fmt, ...) {
55 * Allocates a contiguous region of pages of the requested size.
56 * Returns the physical address of the first page in the region.
58 static void * palacios_allocate_pages(int num_pages, unsigned int alignment) {
59 void * pg_addr = NULL;
61 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
62 pg_allocs += num_pages;
69 * Frees a page previously allocated via palacios_allocate_page().
70 * Note that palacios_allocate_page() can allocate multiple pages with
71 * a single call while palacios_free_page() only frees a single page.
74 static void palacios_free_pages(void * page_paddr, int num_pages) {
75 pg_frees += num_pages;
76 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
81 * Allocates 'size' bytes of kernel memory.
82 * Returns the kernel virtual address of the memory allocated.
85 palacios_alloc(unsigned int size) {
88 if (irqs_disabled()) {
89 addr = kmalloc(size, GFP_ATOMIC);
91 addr = kmalloc(size, GFP_KERNEL);
100 * Frees memory that was previously allocated by palacios_alloc().
113 * Converts a kernel virtual address to the corresponding physical address.
116 palacios_vaddr_to_paddr(
120 return (void*) __pa(vaddr);
125 * Converts a physical address to the corresponding kernel virtual address.
128 palacios_paddr_to_vaddr(
136 * Runs a function on the specified CPU.
139 // For now, do call only on local CPU
143 void (*fn)(void *arg),
149 // We set wait to 1, but I'm not sure this is necessary
150 smp_call_function_single(cpu_id, fn, arg, 1);
155 struct lnx_thread_arg {
156 int (*fn)(void * arg);
160 static int lnx_thread_target(void * arg) {
161 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
164 printk("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
166 daemonize(thread_info->name);
167 allow_signal(SIGKILL);
171 ret = thread_info->fn(thread_info->arg);
178 return 0; // should not get here.
182 * Creates a kernel thread.
185 palacios_start_kernel_thread(
186 int (*fn) (void * arg),
188 char * thread_name) {
190 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
192 thread_info->fn = fn;
193 thread_info->arg = arg;
195 return kthread_run( lnx_thread_target, thread_info, thread_name );
200 * Starts a kernel thread on the specified CPU.
203 palacios_start_thread_on_cpu(int cpu_id,
204 int (*fn)(void * arg),
206 char * thread_name ) {
207 struct task_struct * thread = NULL;
208 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
210 thread_info->fn = fn;
211 thread_info->arg = arg;
214 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
216 if (IS_ERR(thread)) {
217 printk("Palacios error creating thread: %s\n", thread_name);
221 set_cpus_allowed_ptr(thread, cpumask_of(cpu_id));
222 wake_up_process(thread);
229 * Rebind a kernel thread to the specified CPU
230 * The thread will be running on target CPU on return
231 * non-zero return means failure
234 palacios_move_thread_to_cpu(int new_cpu_id,
236 struct task_struct * thread = (struct task_struct *)thread_ptr;
238 printk("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
240 if (thread == NULL) {
245 * Bind to the specified CPU. When this call returns,
246 * the thread should be running on the target CPU.
248 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
253 * Returns the CPU ID that the caller is running on.
256 palacios_get_cpu(void)
259 /* We want to call smp_processor_id()
260 * But this is not safe if kernel preemption is possible
261 * We need to ensure that the palacios threads are bound to a give cpu
264 unsigned int cpu_id = get_cpu();
270 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
273 * This is dependent on the implementation of xcall_reschedule(). Currently
274 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
275 * but instead relies on the return to user space to handle it. Because
276 * palacios is a kernel thread schedule will not be called, which is correct.
277 * If it ever changes to induce side effects, we'll need to figure something
281 #include <asm/apic.h>
284 palacios_interrupt_cpu(
285 struct v3_vm_info * vm,
291 smp_send_reschedule(cpu_id);
293 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
299 * Dispatches an interrupt to Palacios for handling.
302 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
303 struct v3_interrupt intr = {
305 .error = regs->orig_ax,
309 if (irq_to_guest_map[vector]) {
310 v3_deliver_irq(irq_to_guest_map[vector], &intr);
316 * Instructs the kernel to forward the specified IRQ to Palacios.
319 palacios_hook_interrupt(struct v3_vm_info * vm,
320 unsigned int vector ) {
321 printk("hooking vector %d\n", vector);
323 if (irq_to_guest_map[vector]) {
325 "%s: Interrupt vector %u is already hooked.\n",
331 "%s: Hooking interrupt vector %u to vm %p.\n",
332 __func__, vector, vm);
334 irq_to_guest_map[vector] = vm;
337 * NOTE: Normally PCI devices are supposed to be level sensitive,
338 * but we need them to be edge sensitive so that they are
339 * properly latched by Palacios. Leaving them as level
340 * sensitive would lead to an interrupt storm.
342 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
344 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
346 panic("unexpected vector for hooking\n");
353 printk("hooking vector: %d\n", vector);
361 error = request_irq((vector - 32),
362 (void *)palacios_dispatch_interrupt,
364 "interrupt_for_palacios",
368 printk("error code for request_irq is %d\n", error);
369 panic("request vector %d failed",vector);
379 * Acknowledges an interrupt.
382 palacios_ack_interrupt(
387 printk("Pretending to ack interrupt, vector=%d\n",vector);
392 * Returns the CPU frequency in kilohertz.
395 palacios_get_cpu_khz(void)
397 printk("cpu_khz is %u\n",cpu_khz);
400 printk("faking cpu_khz to 1000000\n");
409 * Yield the CPU so other host OS tasks can run.
412 palacios_yield_cpu(void)
422 * Returns NULL on failure.
425 palacios_mutex_alloc(void)
427 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
430 spin_lock_init(lock);
440 palacios_mutex_free(void * mutex) {
448 palacios_mutex_lock(void * mutex, int must_spin) {
449 spin_lock((spinlock_t *)mutex);
456 palacios_mutex_unlock(
460 spin_unlock((spinlock_t *)mutex);
464 * Structure used by the Palacios hypervisor to interface with the host kernel.
466 static struct v3_os_hooks palacios_os_hooks = {
467 .print = palacios_print,
468 .allocate_pages = palacios_allocate_pages,
469 .free_pages = palacios_free_pages,
470 .malloc = palacios_alloc,
471 .free = palacios_free,
472 .vaddr_to_paddr = palacios_vaddr_to_paddr,
473 .paddr_to_vaddr = palacios_paddr_to_vaddr,
474 .hook_interrupt = palacios_hook_interrupt,
475 .ack_irq = palacios_ack_interrupt,
476 .get_cpu_khz = palacios_get_cpu_khz,
477 .start_kernel_thread = palacios_start_kernel_thread,
478 .yield_cpu = palacios_yield_cpu,
479 .mutex_alloc = palacios_mutex_alloc,
480 .mutex_free = palacios_mutex_free,
481 .mutex_lock = palacios_mutex_lock,
482 .mutex_unlock = palacios_mutex_unlock,
483 .get_cpu = palacios_get_cpu,
484 .interrupt_cpu = palacios_interrupt_cpu,
485 .call_on_cpu = palacios_xcall,
486 .start_thread_on_cpu = palacios_start_thread_on_cpu,
487 .move_thread_to_cpu = palacios_move_thread_to_cpu,
493 int palacios_vmm_init( void )
496 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
498 printk("palacios_init starting - calling init_v3\n");
500 Init_V3(&palacios_os_hooks, num_online_cpus());
507 int palacios_vmm_exit( void ) {