Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


f4196136745d0105b27cd782d71fc7752903068f
[palacios.git] / palacios / src / geekos / vmm_stubs.c
1 #include <geekos/vmm_stubs.h>
2 #include <geekos/serial.h>
3 #include <palacios/vm_guest.h>
4 #include <geekos/debug.h>
5
6
7
8 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
9 {
10     __asm__ __volatile__ (
11         "outb %b0, %w1"
12         :
13         : "a" (value), "Nd" (port)
14     );
15 }
16
17 /*
18  * Read a byte from an I/O port.
19  */
20 static inline uchar_t VM_In_Byte(ushort_t port)
21 {
22     uchar_t value;
23
24     __asm__ __volatile__ (
25         "inb %w1, %b0"
26         : "=a" (value)
27         : "Nd" (port)
28     );
29
30     return value;
31 }
32
33
34
35
36 void * Identity(void *addr) { return addr; };
37
38 void * Allocate_VMM_Pages(int num_pages) {
39   void * start_page = Alloc_Page();
40   //SerialPrint("Starting by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages); 
41   int i = 1;
42
43   while (i < num_pages) {
44     void * tmp_page = Alloc_Page();
45     //SerialPrint("Allocating Page: %x (%d of %d)\n",tmp_page, i+1, num_pages); 
46     
47     if (tmp_page != start_page + (PAGE_SIZE * i)) {
48       //we have to start over...;
49       while (i >= 0) {
50         Free_Page(start_page + (PAGE_SIZE * i));
51         i--;
52       }
53       start_page = Alloc_Page();
54       //SerialPrint("Starting over by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
55       i = 1;
56       continue;
57     }
58     i++;
59   }
60
61   return start_page;
62 }
63
64 void Free_VMM_Page(void * page) {
65   Free_Page(page);
66 }
67
68
69 void * VMM_Malloc(unsigned int size) {
70   return Malloc((unsigned long) size);
71 }
72
73
74 void VMM_Free(void * addr) {
75   Free(addr);
76 }
77
78 //
79 //
80 // This is the interrupt state that the VMM's interrupt handlers need to see
81 //
82 struct vmm_intr_state {
83   uint_t irq;
84   uint_t error;
85
86   uint_t should_ack;  // Should the vmm ack this interrupt, or will
87                       // the host OS do it?
88
89   // This is the value given when the interrupt is hooked.
90   // This will never be NULL
91   void *opaque;
92 };
93
94 // This is the function the interface code should call to deliver
95 // the interrupt to the vmm for handling
96 extern void deliver_interrupt_to_vmm(struct vmm_intr_state *state);
97
98
99 struct guest_info * irq_map[256];
100
101 void *my_opaque[256];
102
103
104 static void translate_intr_handler(struct Interrupt_State *state)
105 {
106
107
108   struct vmm_intr_state mystate;
109
110   mystate.irq=state->intNum-32;
111   mystate.error=state->errorCode;
112   mystate.should_ack=0;
113   mystate.opaque=my_opaque[mystate.irq];
114
115   //  PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
116
117   deliver_interrupt_to_vmm(&mystate);
118
119   End_IRQ(state);
120
121 }
122
123 static void pic_intr_handler(struct Interrupt_State * state) {
124   Begin_IRQ(state);
125   struct guest_info * info =   irq_map[state->intNum - 32];
126   SerialPrint("Interrupt %d (IRQ=%d)\n", state->intNum, state->intNum - 32);
127
128   if (info) {
129     info->vm_ops.raise_irq(info, state->intNum - 32);
130   } else {
131     SerialPrint("Interrupt handler error: NULL pointer found, no action taken\n");
132     End_IRQ(state);
133     return;
134   }
135
136   // End_IRQ(state);
137 }
138
139 //
140 //
141 // I really don't know what the heck this is doing... PAD
142 //
143 int hook_irq_stub(struct guest_info * info, int irq) {
144   if (irq_map[irq]) {
145     return -1;
146   }
147
148   SerialPrint("Hooking IRQ: %d (vm=0x%x)\n", irq, info);
149   irq_map[irq] = info;
150   volatile void *foo = pic_intr_handler;
151
152   /* This is disabled for the time being */
153   foo = 0;
154
155
156   Disable_IRQ(irq);
157   Install_IRQ(irq, pic_intr_handler);
158   Enable_IRQ(irq);
159   return 0;
160 }
161
162
163 int geekos_hook_interrupt_new(uint_t irq, void * opaque)
164 {
165   if (my_opaque[irq]) { 
166     PrintBoth("Attempt to hook interrupt that is already hooked\n");
167     return -1;
168   } else {
169     PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n",irq,opaque);
170     my_opaque[irq]=opaque;
171   }
172
173   Disable_IRQ(irq);
174   Install_IRQ(irq,translate_intr_handler);
175   Enable_IRQ(irq);
176   return 0;
177 }
178
179
180 int ack_irq(int irq) {
181   End_IRQ_num(irq);
182   return 0;
183 }
184
185   
186 void Init_Stubs() {
187   memset(irq_map, 0, sizeof(struct guest_info *) * 256);
188 }
189
190
191 unsigned int get_cpu_khz() {
192   extern uint_t cpu_khz_freq;
193
194   unsigned long  print_khz = (unsigned long)(cpu_khz_freq & 0xffffffff);
195   
196   PrintBoth("Detected %lu.%lu MHz CPU\n", print_khz / 1000, print_khz % 1000);
197
198   return cpu_khz_freq;
199 }
200