1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #include <geekos/vmm_stubs.h>
5 #include <geekos/serial.h>
6 #include <palacios/vm_guest.h>
7 #include <geekos/debug.h>
11 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
13 __asm__ __volatile__ (
16 : "a" (value), "Nd" (port)
21 * Read a byte from an I/O port.
23 static inline uchar_t VM_In_Byte(ushort_t port)
27 __asm__ __volatile__ (
39 void * Identity(void *addr) { return addr; };
41 void * Allocate_VMM_Pages(int num_pages) {
42 void * start_page = Alloc_Page();
43 //SerialPrint("Starting by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
46 while (i < num_pages) {
47 void * tmp_page = Alloc_Page();
48 //SerialPrint("Allocating Page: %x (%d of %d)\n",tmp_page, i+1, num_pages);
50 if (tmp_page != start_page + (PAGE_SIZE * i)) {
51 //we have to start over...;
53 Free_Page(start_page + (PAGE_SIZE * i));
56 start_page = Alloc_Page();
57 //SerialPrint("Starting over by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
67 void Free_VMM_Page(void * page) {
72 void * VMM_Malloc(unsigned int size) {
73 return Malloc((unsigned long) size);
77 void VMM_Free(void * addr) {
83 // This is the interrupt state that the VMM's interrupt handlers need to see
85 struct vmm_intr_state {
89 uint_t should_ack; // Should the vmm ack this interrupt, or will
92 // This is the value given when the interrupt is hooked.
93 // This will never be NULL
97 // This is the function the interface code should call to deliver
98 // the interrupt to the vmm for handling
99 extern void deliver_interrupt_to_vmm(struct vmm_intr_state *state);
102 struct guest_info * irq_map[256];
104 void *my_opaque[256];
107 static void translate_intr_handler(struct Interrupt_State *state)
111 struct vmm_intr_state mystate;
113 mystate.irq=state->intNum-32;
114 mystate.error=state->errorCode;
115 mystate.should_ack=0;
116 mystate.opaque=my_opaque[mystate.irq];
118 // PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
120 deliver_interrupt_to_vmm(&mystate);
128 static void pic_intr_handler(struct Interrupt_State * state) {
130 struct guest_info * info = irq_map[state->intNum - 32];
131 SerialPrint("Interrupt %d (IRQ=%d)\n", state->intNum, state->intNum - 32);
134 info->vm_ops.raise_irq(info, state->intNum - 32);
136 SerialPrint("Interrupt handler error: NULL pointer found, no action taken\n");
146 // I really don't know what the heck this is doing... PAD
149 int hook_irq_stub(struct guest_info * info, int irq) {
154 SerialPrint("Hooking IRQ: %d (vm=0x%x)\n", irq, info);
156 volatile void *foo = pic_intr_handler;
158 // This is disabled for the time being
163 Install_IRQ(irq, pic_intr_handler);
169 int geekos_hook_interrupt_new(uint_t irq, void * opaque)
171 if (my_opaque[irq]) {
172 PrintBoth("Attempt to hook interrupt that is already hooked\n");
175 PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n",irq,opaque);
176 my_opaque[irq]=opaque;
180 Install_IRQ(irq,translate_intr_handler);
186 int ack_irq(int irq) {
193 memset(irq_map, 0, sizeof(struct guest_info *) * 256);
197 unsigned int get_cpu_khz() {
198 extern uint_t cpu_khz_freq;
200 unsigned long print_khz = (unsigned long)(cpu_khz_freq & 0xffffffff);
202 PrintBoth("Detected %lu.%lu MHz CPU\n", print_khz / 1000, print_khz % 1000);