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;
44 * Prints a message to the console.
46 static void palacios_print(const char * fmt, ...) {
58 * Allocates a contiguous region of pages of the requested size.
59 * Returns the physical address of the first page in the region.
61 static void * palacios_allocate_pages(int num_pages, unsigned int alignment) {
62 void * pg_addr = NULL;
64 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
65 pg_allocs += num_pages;
72 * Frees a page previously allocated via palacios_allocate_page().
73 * Note that palacios_allocate_page() can allocate multiple pages with
74 * a single call while palacios_free_page() only frees a single page.
77 static void palacios_free_pages(void * page_paddr, int num_pages) {
78 pg_frees += num_pages;
79 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
84 * Allocates 'size' bytes of kernel memory.
85 * Returns the kernel virtual address of the memory allocated.
88 palacios_alloc(unsigned int size) {
91 if (irqs_disabled()) {
92 addr = kmalloc(size, GFP_ATOMIC);
94 addr = kmalloc(size, GFP_KERNEL);
103 * Frees memory that was previously allocated by palacios_alloc().
116 * Converts a kernel virtual address to the corresponding physical address.
119 palacios_vaddr_to_paddr(
123 return (void*) __pa(vaddr);
128 * Converts a physical address to the corresponding kernel virtual address.
131 palacios_paddr_to_vaddr(
139 * Runs a function on the specified CPU.
142 // For now, do call only on local CPU
146 void (*fn)(void *arg),
152 // We set wait to 1, but I'm not sure this is necessary
153 smp_call_function_single(cpu_id, fn, arg, 1);
158 struct lnx_thread_arg {
159 int (*fn)(void * arg);
163 static int lnx_thread_target(void * arg) {
164 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
167 printk("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
169 daemonize(thread_info->name);
170 allow_signal(SIGKILL);
174 ret = thread_info->fn(thread_info->arg);
181 return 0; // should not get here.
185 * Creates a kernel thread.
188 palacios_start_kernel_thread(
189 int (*fn) (void * arg),
191 char * thread_name) {
193 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
195 thread_info->fn = fn;
196 thread_info->arg = arg;
198 return kthread_run( lnx_thread_target, thread_info, thread_name );
203 * Starts a kernel thread on the specified CPU.
206 palacios_start_thread_on_cpu(int cpu_id,
207 int (*fn)(void * arg),
209 char * thread_name ) {
210 struct task_struct * thread = NULL;
211 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
213 thread_info->fn = fn;
214 thread_info->arg = arg;
217 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
219 if (IS_ERR(thread)) {
220 printk("Palacios error creating thread: %s\n", thread_name);
224 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
225 kthread_stop(thread);
229 wake_up_process(thread);
236 * Rebind a kernel thread to the specified CPU
237 * The thread will be running on target CPU on return
238 * non-zero return means failure
241 palacios_move_thread_to_cpu(int new_cpu_id,
243 struct task_struct * thread = (struct task_struct *)thread_ptr;
245 printk("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
247 if (thread == NULL) {
252 * Bind to the specified CPU. When this call returns,
253 * the thread should be running on the target CPU.
255 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
260 * Returns the CPU ID that the caller is running on.
263 palacios_get_cpu(void)
266 /* We want to call smp_processor_id()
267 * But this is not safe if kernel preemption is possible
268 * We need to ensure that the palacios threads are bound to a give cpu
271 unsigned int cpu_id = get_cpu();
277 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
280 * This is dependent on the implementation of xcall_reschedule(). Currently
281 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
282 * but instead relies on the return to user space to handle it. Because
283 * palacios is a kernel thread schedule will not be called, which is correct.
284 * If it ever changes to induce side effects, we'll need to figure something
288 #include <asm/apic.h>
291 palacios_interrupt_cpu(
292 struct v3_vm_info * vm,
298 smp_send_reschedule(cpu_id);
300 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
306 * Dispatches an interrupt to Palacios for handling.
309 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
310 struct v3_interrupt intr = {
312 .error = regs->orig_ax,
316 if (irq_to_guest_map[vector]) {
317 v3_deliver_irq(irq_to_guest_map[vector], &intr);
323 * Instructs the kernel to forward the specified IRQ to Palacios.
326 palacios_hook_interrupt(struct v3_vm_info * vm,
327 unsigned int vector ) {
328 printk("hooking vector %d\n", vector);
330 if (irq_to_guest_map[vector]) {
332 "%s: Interrupt vector %u is already hooked.\n",
338 "%s: Hooking interrupt vector %u to vm %p.\n",
339 __func__, vector, vm);
341 irq_to_guest_map[vector] = vm;
344 * NOTE: Normally PCI devices are supposed to be level sensitive,
345 * but we need them to be edge sensitive so that they are
346 * properly latched by Palacios. Leaving them as level
347 * sensitive would lead to an interrupt storm.
349 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
351 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
353 panic("unexpected vector for hooking\n");
360 printk("hooking vector: %d\n", vector);
368 error = request_irq((vector - 32),
369 (void *)palacios_dispatch_interrupt,
371 "interrupt_for_palacios",
375 printk("error code for request_irq is %d\n", error);
376 panic("request vector %d failed",vector);
386 * Acknowledges an interrupt.
389 palacios_ack_interrupt(
394 printk("Pretending to ack interrupt, vector=%d\n",vector);
399 * Returns the CPU frequency in kilohertz.
402 palacios_get_cpu_khz(void)
404 printk("cpu_khz is %u\n",cpu_khz);
407 printk("faking cpu_khz to 1000000\n");
416 * Yield the CPU so other host OS tasks can run.
419 palacios_yield_cpu(void)
429 * Returns NULL on failure.
432 palacios_mutex_alloc(void)
434 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
437 spin_lock_init(lock);
447 palacios_mutex_free(void * mutex) {
455 palacios_mutex_lock(void * mutex, int must_spin) {
456 spin_lock((spinlock_t *)mutex);
463 palacios_mutex_unlock(
467 spin_unlock((spinlock_t *)mutex);
471 * Structure used by the Palacios hypervisor to interface with the host kernel.
473 static struct v3_os_hooks palacios_os_hooks = {
474 .print = palacios_print,
475 .allocate_pages = palacios_allocate_pages,
476 .free_pages = palacios_free_pages,
477 .malloc = palacios_alloc,
478 .free = palacios_free,
479 .vaddr_to_paddr = palacios_vaddr_to_paddr,
480 .paddr_to_vaddr = palacios_paddr_to_vaddr,
481 .hook_interrupt = palacios_hook_interrupt,
482 .ack_irq = palacios_ack_interrupt,
483 .get_cpu_khz = palacios_get_cpu_khz,
484 .start_kernel_thread = palacios_start_kernel_thread,
485 .yield_cpu = palacios_yield_cpu,
486 .mutex_alloc = palacios_mutex_alloc,
487 .mutex_free = palacios_mutex_free,
488 .mutex_lock = palacios_mutex_lock,
489 .mutex_unlock = palacios_mutex_unlock,
490 .get_cpu = palacios_get_cpu,
491 .interrupt_cpu = palacios_interrupt_cpu,
492 .call_on_cpu = palacios_xcall,
493 .start_thread_on_cpu = palacios_start_thread_on_cpu,
494 .move_thread_to_cpu = palacios_move_thread_to_cpu,
500 int palacios_vmm_init( void )
502 int num_cpus = num_online_cpus();
503 char * cpu_mask = NULL;
505 if (cpu_list_len > 0) {
510 cpu_mask = kmalloc((num_cpus / 8) + 1, GFP_KERNEL);
511 memset(cpu_mask, 0, (num_cpus / 8) + 1);
513 for (i = 0; i < cpu_list_len; i++) {
514 if (cpu_list[i] >= num_cpus) {
515 printk("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
519 major = cpu_list[i] / 8;
520 minor = cpu_list[i] % 8;
522 *(cpu_mask + major) |= (0x1 << minor);
526 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
528 printk("palacios_init starting - calling init_v3\n");
530 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus);
537 int palacios_vmm_exit( void ) {