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.


c83e6d5e87664fef3760133130127e6ddf996d4e
[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 struct guest_info * irq_map[256];
81
82 static void pic_intr_handler(struct Interrupt_State * state) {
83   Begin_IRQ(state);
84   struct guest_info * info =   irq_map[state->intNum - 32];
85   SerialPrint("Interrupt %d (IRQ=%d)\n", state->intNum, state->intNum - 32);
86
87   if (info) {
88     info->vm_ops.raise_irq(info, state->intNum - 32);
89   } else {
90     SerialPrint("Interrupt handler error: NULL pointer found, no action taken\n");
91     End_IRQ(state);
92     return;
93   }
94
95   // End_IRQ(state);
96 }
97
98
99 int hook_irq_stub(struct guest_info * info, int irq) {
100   if (irq_map[irq]) {
101     return -1;
102   }
103
104   SerialPrint("Hooking IRQ: %d (vm=0x%x)\n", irq, info);
105   irq_map[irq] = info;
106   volatile void *foo = pic_intr_handler;
107   foo=0;
108   Disable_IRQ(irq);
109   Install_IRQ(irq, pic_intr_handler);
110   Enable_IRQ(irq);
111   return 0;
112 }
113
114
115 int ack_irq(int irq) {
116   End_IRQ_num(irq);
117   return 0;
118 }
119
120   
121 void Init_Stubs() {
122   memset(irq_map, 0, sizeof(struct guest_info *) * 256);
123 }
124
125
126 unsigned int get_cpu_khz() {
127   extern uint_t cpu_khz_freq;
128
129   unsigned long  print_khz = (unsigned long)(cpu_khz_freq & 0xffffffff);
130   
131   PrintBoth("Detected %lu.%lu MHz CPU\n", print_khz / 1000, print_khz % 1000);
132
133   return cpu_khz_freq;
134 }
135
136
137
138
139 #if 0
140
141
142 /* ------ Calibrate the TSC ------- 
143  * Return processor ticks per second / CALIBRATE_FRAC.
144  *
145  * Ported From Xen
146  */
147 #define PIT_MODE 0x43
148 #define PIT_CH2 0x42
149 #define CLOCK_TICK_RATE 1193180 /* system crystal frequency (Hz) */
150 #define CALIBRATE_FRAC  20      /* calibrate over 50ms */
151 #define CALIBRATE_LATCH ((CLOCK_TICK_RATE+(CALIBRATE_FRAC/2))/CALIBRATE_FRAC)
152
153
154 unsigned long long get_cpu_khz() {
155     ullong_t start, end;
156     unsigned long count;
157     unsigned long long tmp;
158     unsigned long print_tmp; 
159
160     /* Set the Gate high, disable speaker */
161     VM_Out_Byte((VM_In_Byte(0x61) & ~0x02) | 0x01, 0x61);
162
163     /*
164      * Now let's take care of CTC channel 2
165      *
166      * Set the Gate high, program CTC channel 2 for mode 0, (interrupt on
167      * terminal count mode), binary count, load 5 * LATCH count, (LSB and MSB)
168      * to begin countdown.
169      */
170     VM_Out_Byte(0xb0, PIT_MODE);           /* binary, mode 0, LSB/MSB, Ch 2 */
171     VM_Out_Byte(CALIBRATE_LATCH & 0xff, PIT_CH2); /* LSB of count */
172     VM_Out_Byte(CALIBRATE_LATCH >> 8, PIT_CH2);   /* MSB of count */
173
174     rdtscll(start);
175     for ( count = 0; (VM_In_Byte(0x61) & 0x20) == 0; count++ )
176         continue;
177     rdtscll(end);
178
179     /* Error if the CTC doesn't behave itself. */
180     if ( count == 0 ) {
181      PrintBoth("CPU Frequency Calibration Error\n");
182       return 0;
183     }
184
185     tmp = ((end - start) * (ullong_t)CALIBRATE_FRAC);
186
187     do_div(tmp, 1000);
188
189     tmp &= 0xffffffff;
190     print_tmp = (unsigned long)tmp;
191
192    PrintBoth("Detected %lu.%lu MHz CPU\n", print_tmp / 1000, print_tmp % 1000);
193     return tmp;
194 }
195
196 #undef PIT_CH2
197 #undef PIT_MODE
198 #undef CLOCK_TICK_RATE
199 #undef CALIBRATE_FRAC  
200 #undef CALIBRATE_LATCH 
201
202 #endif