2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <geekos/vmm_stubs.h>
21 #include <geekos/serial.h>
22 #include <palacios/vm_guest.h>
23 #include <geekos/debug.h>
27 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
29 __asm__ __volatile__ (
32 : "a" (value), "Nd" (port)
37 * Read a byte from an I/O port.
39 static inline uchar_t VM_In_Byte(ushort_t port)
43 __asm__ __volatile__ (
55 void * Identity(void *addr) { return addr; };
57 void * Allocate_VMM_Pages(int num_pages) {
58 void * start_page = Alloc_Page();
59 //SerialPrint("Starting by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
62 while (i < num_pages) {
63 void * tmp_page = Alloc_Page();
64 //SerialPrint("Allocating Page: %x (%d of %d)\n",tmp_page, i+1, num_pages);
66 if (tmp_page != start_page + (PAGE_SIZE * i)) {
67 //we have to start over...;
69 Free_Page(start_page + (PAGE_SIZE * i));
72 start_page = Alloc_Page();
73 //SerialPrint("Starting over by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
83 void Free_VMM_Page(void * page) {
88 void * VMM_Malloc(unsigned int size) {
89 return Malloc((unsigned long) size);
93 void VMM_Free(void * addr) {
99 // This is the interrupt state that the VMM's interrupt handlers need to see
101 struct vmm_intr_state {
105 uint_t should_ack; // Should the vmm ack this interrupt, or will
106 // the host OS do it?
108 // This is the value given when the interrupt is hooked.
109 // This will never be NULL
113 // This is the function the interface code should call to deliver
114 // the interrupt to the vmm for handling
115 extern void deliver_interrupt_to_vmm(struct vmm_intr_state *state);
118 struct guest_info * irq_map[256];
120 void *my_opaque[256];
123 static void translate_intr_handler(struct Interrupt_State *state)
127 struct vmm_intr_state mystate;
129 mystate.irq=state->intNum-32;
130 mystate.error=state->errorCode;
131 mystate.should_ack=0;
132 mystate.opaque=my_opaque[mystate.irq];
134 // PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
136 deliver_interrupt_to_vmm(&mystate);
144 static void pic_intr_handler(struct Interrupt_State * state) {
146 struct guest_info * info = irq_map[state->intNum - 32];
147 SerialPrint("Interrupt %d (IRQ=%d)\n", state->intNum, state->intNum - 32);
150 info->vm_ops.raise_irq(info, state->intNum - 32);
152 SerialPrint("Interrupt handler error: NULL pointer found, no action taken\n");
162 // I really don't know what the heck this is doing... PAD
165 int hook_irq_stub(struct guest_info * info, int irq) {
170 SerialPrint("Hooking IRQ: %d (vm=0x%x)\n", irq, info);
172 volatile void *foo = pic_intr_handler;
174 // This is disabled for the time being
179 Install_IRQ(irq, pic_intr_handler);
185 int geekos_hook_interrupt_new(uint_t irq, void * opaque)
187 if (my_opaque[irq]) {
188 PrintBoth("Attempt to hook interrupt that is already hooked\n");
191 PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n",irq,opaque);
192 my_opaque[irq]=opaque;
196 Install_IRQ(irq,translate_intr_handler);
202 int ack_irq(int irq) {
209 memset(irq_map, 0, sizeof(struct guest_info *) * 256);
213 unsigned int get_cpu_khz() {
214 extern uint_t cpu_khz_freq;
216 unsigned long print_khz = (unsigned long)(cpu_khz_freq & 0xffffffff);
218 PrintBoth("Detected %lu.%lu MHz CPU\n", print_khz / 1000, print_khz % 1000);