1 #include <nautilus/nautilus.h>
2 #include <nautilus/printk.h>
3 #include <nautilus/thread.h>
4 #include <nautilus/cpu.h>
5 #include <nautilus/mm.h>
9 #include <palacios/vmm.h>
12 #include "palacios-nautilus-mm.h"
17 This is a simple proof-of-concept implementation of the Palacios
18 host interface on top of Nautilus. It is sufficient to allow
19 us to boot a guest OS running Linux. A few things to note:
21 - Nautilus currently has a grand-unified allocator designed to help
22 support parallel run-time integration. All of alloc/valloc/page
23 allocation are built on top of that. See palacios-nautilus-mm.c
24 for how this works for page allocation.
25 - For page allocation, constraints, NUMA, and filter expressions are
27 - To make this work, you also need updates on the Nautilus side.
28 (these will eventually get into the Nautilus repo)
29 - thread migration is not supported currently
30 - hooking of host interrupts is not supported currently.
31 - Palacios can sleep, yield, wakeup, etc, but be aware
32 that Nautilus threads operate differently than those of
36 - Do Nautilus regular startup to bring all cores to idle
37 - From a kernel thread, ideally the init thread on core 0,
38 do palacios_vmm_init(memory_size_bytes,options)
39 - Create, launch, etc, VMs using the Palacios v3_* functions
40 (note that these are NOT wrapped here)
41 - Console assumes void *the_vm is defined in the host, and it
42 is whatever a v3_create_vm() returned. This is the VM that
43 has console access (keyboard and screen).
44 - After you are done, do a palacios_vmm_deinit();
48 // The following can be used to track memory bugs
49 // zero memory after allocation (now applies to valloc and page alloc as well)
50 #define ALLOC_ZERO_MEM 1
51 // pad allocations by this many bytes on both ends of block (heap only)
53 #define MAX_THREAD_NAME 32
56 int run_nk_thread = 0;
58 static struct v3_vm_info * irq_to_guest_map[256];
60 static unsigned int cpu_khz=-1;
62 static char *print_buffer[NR_CPUS];
64 static void deinit_print_buffers(void)
68 for (i=0;i<NR_CPUS;i++) {
69 if (print_buffer[i]) {
70 palacios_free(print_buffer[i]);
76 static int init_print_buffers(void)
80 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
83 for (i=0;i<NR_CPUS;i++) {
84 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
85 if (!print_buffer[i]) {
86 ERROR("Cannot allocate print buffer for cpu %d\n",i);
87 deinit_print_buffers();
90 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
101 * Prints a message to the console.
103 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...)
107 unsigned int cpu = palacios_get_cpu();
108 char *buf = cpu < NR_CPUS ? print_buffer[cpu] : 0;
112 printk("palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
118 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
123 printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
129 printk(KERN_INFO "palacios (pcore %u vm %s): %s",
135 printk(KERN_INFO "palacios (pcore %u): %s",
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, int node_id, int (*filter_func)(void *paddr, void *filter_state), void *filter_state)
151 void * pg_addr = NULL;
154 ERROR("ALERT ALERT Attempt to allocate zero or fewer pages (%d pages, alignment %d, node %d, filter_func %p, filter_state %p)\n",num_pages, alignment, node_id, filter_func, filter_state);
158 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment, node_id, filter_func, filter_state);
161 ERROR("ALERT ALERT Page allocation has FAILED Warning (%d pages, alignment %d, node %d, filter_func %p, filter_state %p)\n",num_pages, alignment, node_id, filter_func, filter_state);
166 memset(pg_addr,0,num_pages*4096);
169 // INFO("allocpages: %p (%llu pages) alignment=%u\n", pg_addr, num_pages, alignment);
176 * Frees a page previously allocated via palacios_allocate_page().
177 * Note that palacios_allocate_page() can allocate multiple pages with
178 * a single call while palacios_free_page() only frees a single page.
181 void palacios_free_pages(void * page_paddr, int num_pages) {
183 ERROR("Ignoring free pages: 0x%p (0x%lx)for %d pages\n", page_paddr, (uintptr_t)page_paddr, num_pages);
186 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
188 // INFO("freepages: %p (%llu pages) alignment=%u\n", page_paddr, num_pages);
193 palacios_alloc_extended(unsigned int size, unsigned int flags, int node) {
197 ERROR("ALERT ALERT attempt to kmalloc zero bytes rejected\n");
202 addr = malloc(size+2*ALLOC_PAD);
204 // currently no numa-zone specific kmalloc
205 addr = malloc(size+2*ALLOC_PAD);
209 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
214 memset(addr,0,size+2*ALLOC_PAD);
217 //INFO("malloc: 0x%p (%llu bytes)\n",addr+ALLOC_PAD,size);
219 return addr+ALLOC_PAD;
223 palacios_valloc(unsigned int size)
228 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
232 // currently no vmalloc
236 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
244 //INFO("valloc: 0x%p (%llu bytes)\n",addr,size);
249 void palacios_vfree(void *p)
252 ERROR("Ignoring vfree: 0x%p\n",p);
255 // no vfree currently
258 //INFO("vfree: 0x%p\n",p);
262 * Allocates 'size' bytes of kernel memory.
263 * Returns the kernel virtual address of the memory allocated.
266 palacios_alloc(unsigned int size)
268 return palacios_alloc_extended(size,0,-1);
272 * Frees memory that was previously allocated by palacios_alloc().
275 palacios_free(void *addr)
279 ERROR("Ignoring free : 0x%p\n", addr);
283 free(addr-ALLOC_PAD);
284 //INFO("free: %p\n",addr-ALLOC_PAD);
288 * Converts a kernel virtual address to the corresponding physical address.
291 palacios_vaddr_to_paddr(
295 return vaddr; // our memory mapping is identity
300 * Converts a physical address to the corresponding kernel virtual address.
303 palacios_paddr_to_vaddr(
307 return paddr; // our memory mapping is identity
311 * Runs a function on the specified CPU.
316 void (*fn)(void *arg),
321 smp_xcall(cpu_id,fn,arg,1);
328 struct nautilus_thread_arg {
329 int (*fn)(void * arg);
331 char name[MAX_THREAD_NAME];
334 static void nautilus_thread_target(void * in, void ** out)
336 struct nautilus_thread_arg * thread_info = (struct nautilus_thread_arg *)in;
339 ret = thread_info->fn(thread_info->arg);
341 INFO("Palacios Thread (%s) EXITING with return code %d\n", thread_info->name, ret);
343 palacios_free(thread_info);
347 * Creates a kernel thread.
350 palacios_create_and_start_kernel_thread(
351 int (*fn) (void * arg),
354 v3_resource_control_t *rctl)
357 struct nautilus_thread_arg * thread_info = palacios_alloc(sizeof(struct nautilus_thread_arg));
358 nk_thread_id_t tid = 0;
361 ERROR("ALERT ALERT Unable to allocate thread\n");
365 thread_info->fn = fn;
366 thread_info->arg = arg;
367 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
368 thread_info->name[MAX_THREAD_NAME-1] =0;
370 nk_thread_start(nautilus_thread_target, thread_info, 0, 0, 0, &tid, CPU_ANY);
377 * Starts a kernel thread on the specified CPU.
380 palacios_create_thread_on_cpu(int cpu_id,
381 int (*fn)(void * arg),
384 v3_resource_control_t *rctl)
386 nk_thread_id_t newtid;
387 nk_thread_t * newthread = NULL;
388 struct nautilus_thread_arg * thread_info = palacios_alloc(sizeof(struct nautilus_thread_arg));
390 thread_info->fn = fn;
391 thread_info->arg = arg;
392 strncpy(thread_info->name, thread_name, MAX_THREAD_NAME);
393 thread_info->name[MAX_THREAD_NAME-1] = 0;
395 //INFO("CREATING A THREAD ON CPU ID: %d\n", cpu_id);
397 if (nk_thread_create(nautilus_thread_target, thread_info, 0, 0, 0, &newtid, cpu_id) < 0) {
398 ERROR("COULD NOT CREATE THREAD\n");
401 //INFO("newtid: %lu\n", newtid);
407 palacios_start_thread(void * th)
416 palacios_create_and_start_thread_on_cpu(int cpu_id,
417 int (*fn)(void * arg),
420 v3_resource_control_t *rctl )
425 struct nautilus_thread_arg * thread_info = palacios_alloc(sizeof(struct nautilus_thread_arg));
428 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
432 thread_info->fn = fn;
433 thread_info->arg = arg;
434 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
435 thread_info->name[MAX_THREAD_NAME-1] =0;
437 nk_thread_start(nautilus_thread_target, thread_info, 0, 0, 0,&tid,cpu_id); //
445 * Rebind a kernel thread to the specified CPU
446 * The thread will be running on target CPU on return
447 * non-zero return means failure
450 palacios_move_thread_to_cpu(int new_cpu_id,
454 INFO("Moving thread (%p) to cpu %d\n", thread_ptr, new_cpu_id);
455 ERROR("NOT CURRENTLY SUPPORTED\n");
461 * Returns the CPU ID that the caller is running on.
464 palacios_get_cpu(void)
470 palacios_interrupt_cpu( struct v3_vm_info * vm,
474 apic_ipi(per_cpu_get(apic),cpu_id,vector); // find out apic_dev * and cpu to apic id mapping
481 * Dispatches an interrupt to Palacios for handling.
484 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
485 struct v3_interrupt intr = {
487 .error = 0, //regs->orig_ax, /* TODO fix this */
491 if (irq_to_guest_map[vector]) {
492 v3_deliver_irq(irq_to_guest_map[vector], &intr);
498 * Instructs the kernel to forward the specified IRQ to Palacios.
501 palacios_hook_interrupt(struct v3_vm_info * vm,
502 unsigned int vector )
504 ERROR("UNSUPPORTED: PALACIOS_HOOK_INTERRUPT\n");
510 * Acknowledges an interrupt.
513 palacios_ack_interrupt(
517 ERROR("UNSUPPORTED: PALACIOS_ACK_INTERRUPT\n");
522 * Returns the CPU frequency in kilohertz.
525 palacios_get_cpu_khz(void)
528 uint32_t cpu = (uint32_t)my_cpu_id();
530 cpu_khz = nk_detect_cpu_freq(cpu);
532 INFO("CANNOT GET THE CPU FREQUENCY. FAKING TO 1000000\n");
536 INFO("Nautilus frequency at %u KHz\n",cpu_khz);
541 * Yield the CPU so other host OS tasks can run.
542 * This will return immediately if there is no other thread that is runnable
543 * And there is no real bound on how long it will yield
546 palacios_yield_cpu(void)
553 * Yield the CPU so other host OS tasks can run.
554 * Given now immediately if there is no other thread that is runnable
555 * And there is no real bound on how long it will yield
557 void palacios_sleep_cpu(unsigned int us)
559 // sleep not supported on Nautilus
565 void palacios_wakeup_cpu(void *thread)
567 // threads never go to sleep, so shouldn't happen
568 ERROR("ERROR ERROR: WAKEUP_CPU CALLED. THREADS ARE NEVER ASLEEP");
574 * Returns NULL on failure.
577 palacios_mutex_alloc(void)
579 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
584 ERROR("ALERT ALERT Unable to allocate lock\n");
591 void palacios_mutex_init(void *mutex)
593 spinlock_t *lock = (spinlock_t*)mutex;
597 LOCKCHECK_ALLOC(lock);
602 void palacios_mutex_deinit(void *mutex)
604 spinlock_t *lock = (spinlock_t*)mutex;
607 spinlock_deinit(lock);
608 LOCKCHECK_FREE(lock);
617 palacios_mutex_free(void * mutex) {
618 palacios_free(mutex);
619 LOCKCHECK_FREE(mutex);
626 palacios_mutex_lock(void * mutex, int must_spin) {
627 LOCKCHECK_LOCK_PRE(mutex);
628 spin_lock((spinlock_t *)mutex);
629 LOCKCHECK_LOCK_POST(mutex);
634 * Locks a mutex, disabling interrupts on this core
637 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
641 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
642 flags = spin_lock_irq_save((spinlock_t *)mutex);
643 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
645 //INFO("lock irqsave flags=%lu\n",flags);
646 return (void *)flags;
654 palacios_mutex_unlock(
658 LOCKCHECK_UNLOCK_PRE(mutex);
659 spin_unlock((spinlock_t *)mutex);
660 LOCKCHECK_UNLOCK_POST(mutex);
665 * Unlocks a mutex and restores previous interrupt state on this core
668 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
670 //INFO("unlock irqrestore flags=%lu\n",(unsigned long)flags);
671 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
672 // This is correct, flags is opaque
673 spin_unlock_irq_restore((spinlock_t *)mutex,(uint8_t) (unsigned long)flags);
674 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
679 * Structure used by the Palacios hypervisor to interface with the host kernel.
681 static struct v3_os_hooks palacios_os_hooks = {
682 .print = palacios_print_scoped,
683 .allocate_pages = palacios_allocate_pages,
684 .free_pages = palacios_free_pages,
685 .vmalloc = palacios_valloc,
686 .vfree = palacios_vfree,
687 .malloc = palacios_alloc,
688 .free = palacios_free,
689 .vaddr_to_paddr = palacios_vaddr_to_paddr,
690 .paddr_to_vaddr = palacios_paddr_to_vaddr,
691 .hook_interrupt = palacios_hook_interrupt,
692 .ack_irq = palacios_ack_interrupt,
693 .get_cpu_khz = palacios_get_cpu_khz,
694 .start_kernel_thread = palacios_create_and_start_kernel_thread,
695 .yield_cpu = palacios_yield_cpu,
696 .sleep_cpu = palacios_sleep_cpu,
697 .wakeup_cpu = palacios_wakeup_cpu,
698 .mutex_alloc = palacios_mutex_alloc,
699 .mutex_free = palacios_mutex_free,
700 .mutex_lock = palacios_mutex_lock,
701 .mutex_unlock = palacios_mutex_unlock,
702 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
703 .mutex_unlock_irqrestore = palacios_mutex_unlock_irqrestore,
704 .get_cpu = palacios_get_cpu,
705 .interrupt_cpu = palacios_interrupt_cpu,
706 .call_on_cpu = palacios_xcall,
707 .create_thread_on_cpu = palacios_create_thread_on_cpu,
708 .start_thread = palacios_start_thread,
709 .move_thread_to_cpu = palacios_move_thread_to_cpu, // unsupported
715 int palacios_vmm_init(uint64_t memsize, char * options)
717 int num_cpus = nautilus_info.sys.num_cpus;
718 char * cpu_mask = NULL;
725 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
728 ERROR("Cannot allocate cpu mask\n");
732 memset(cpu_mask, 0, (num_cpus / 8) + 1);
734 for (i = 0; i < num_cpus; i++) {
739 *(cpu_mask + major) |= (0x1 << minor);
743 INFO("calling palacios-mm init\n");
744 if (init_palacios_nautilus_mm(memsize)) {
745 ERROR("Failted to initialize memory management\n");
748 INFO("palacios-mm init done\n");
750 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
752 if (init_print_buffers()) {
753 INFO("Cannot initialize print buffers\n");
754 palacios_free(cpu_mask);
758 INFO("printbuffer init done\n");
760 //palacios_print_scoped(0, 0, "Hi%llu\n", 134217728);
762 INFO("NR_CPU: %d\n", NR_CPUS);
764 INFO("palacios_init starting - calling init_v3\n");
766 INFO("calling init_v3 = %p\n", Init_V3);
768 INFO("num_cpus: %d\ncpu_mask: %x\noptions: %s\n", num_cpus, *cpu_mask, options);
770 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
772 INFO("init_v3 done\n");
774 #ifdef V3_CONFIG_CONSOLE
775 INFO("Initializing console\n");
776 nautilus_console_init();
785 int palacios_vmm_exit( void )
788 #ifdef V3_CONFIG_CONSOLE
789 nautilus_console_deinit();
794 INFO("palacios shutdown complete\n");
796 deinit_print_buffers();
798 deinit_palacios_nautilus_mm(); // free memory from the allocator