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.


added generic VMM framework
[palacios.git] / palacios / src / geekos / main.c
1 /*
2  * GeekOS C code entry point
3  * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4  * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
5  * Copyright (c) 2004, Iulian Neamtiu <neamtiu@cs.umd.edu>
6  * $Revision: 1.10 $
7  * 
8  * This is free software.  You are permitted to use,
9  * redistribute, and modify it as specified in the file "COPYING".
10  */
11
12 #include <geekos/bootinfo.h>
13 #include <geekos/string.h>
14 #include <geekos/screen.h>
15 #include <geekos/mem.h>
16 #include <geekos/crc32.h>
17 #include <geekos/tss.h>
18 #include <geekos/int.h>
19 #include <geekos/kthread.h>
20 #include <geekos/trap.h>
21 #include <geekos/timer.h>
22 #include <geekos/keyboard.h>
23 #include <geekos/io.h>
24 #include <geekos/serial.h>
25 #include <geekos/reboot.h>
26 #include <geekos/mem.h>
27 #include <geekos/paging.h>
28 #include <geekos/ide.h>
29
30 #include <geekos/vmm.h>
31
32 #include <geekos/gdt.h>
33
34 #include <geekos/vmm_sizes.h>
35
36 /*
37   static inline unsigned int cpuid_ecx(unsigned int op)
38   {
39   unsigned int eax, ecx;
40   
41   __asm__("cpuid"
42   : "=a" (eax), "=c" (ecx)
43   : "0" (op)
44   : "bx", "dx" );
45   
46   return ecx;
47   }
48 */
49
50
51
52 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
53 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
54 extern uint_t Get_EIP();
55 extern uint_t Get_ESP();
56 extern uint_t Get_EBP();
57
58
59 int foo=42;
60
61 #define SPEAKER_PORT 0x61
62
63
64 void Buzz(unsigned delay, unsigned num)
65 {
66   volatile int x;
67   int i,j;
68   unsigned char init;
69   
70   init=In_Byte(SPEAKER_PORT);
71
72   for (i=0;i<num;i++) { 
73     Out_Byte(SPEAKER_PORT, init|0x2);
74     for (j=0;j<delay;j++) { 
75       x+=j;
76     }
77     Out_Byte(SPEAKER_PORT, init);
78     for (j=0;j<delay;j++) { 
79       x+=j;
80     }
81   }
82 }
83
84 inline void MyOut_Byte(ushort_t port, uchar_t value)
85 {
86     __asm__ __volatile__ (
87         "outb %b0, %w1"
88         :
89         : "a" (value), "Nd" (port)
90     );
91 }
92
93 /*
94  * Read a byte from an I/O port.
95  */
96 inline uchar_t MyIn_Byte(ushort_t port)
97 {
98     uchar_t value;
99
100     __asm__ __volatile__ (
101         "inb %w1, %b0"
102         : "=a" (value)
103         : "Nd" (port)
104     );
105
106     return value;
107 }
108
109
110 extern void MyBuzzVM();
111
112 #define MYBUZZVM_START MyBuzzVM
113 #define MYBUZZVM_LEN   0x3d
114
115 void BuzzVM()
116 {
117   int x;
118   int j;
119   unsigned char init;
120   
121   
122   init=MyIn_Byte(SPEAKER_PORT);
123
124   while (1) {
125     MyOut_Byte(SPEAKER_PORT, init|0x2);
126     for (j=0;j<1000000;j++) { 
127       x+=j;
128     }
129     MyOut_Byte(SPEAKER_PORT, init);
130     for (j=0;j<1000000;j++) { 
131       x+=j;
132     }
133   }
134 }
135
136 extern void RunVM();
137
138 int vmRunning = 0;
139
140 void RunVM() {
141   vmRunning = 1;
142
143   while(1);
144 }
145
146
147
148
149 extern uint_t VMCS_STORE();
150 extern uint_t VMCS_READ();
151
152
153
154 void Buzzer(ulong_t arg) {
155   ulong_t *doIBuzz = (ulong_t*)arg;
156   while (1) {
157     // Quick and dirty hack to save my hearing...
158     // I'm not too worried about timing, so I'll deal with concurrency later...
159     if (*doIBuzz == 1) {
160       Buzz(1000000, 10);
161     }
162   }
163
164 }
165
166
167
168 void Hello(ulong_t arg)
169 {
170   char *b="hello ";
171   char byte;
172   short port=0xe9;
173   int i;
174   while(1){
175     for (i=0;i<6;i++) { 
176       byte=b[i];
177       __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
178     }
179   }
180 }
181
182 void Keyboard_Listener(ulong_t arg) {
183   ulong_t * doIBuzz = (ulong_t*)arg;
184   Keycode key_press;
185
186   Print("Press F4 to turn on/off the speaker\n");
187
188   while ((key_press = Wait_For_Key())) {    
189     if (key_press == KEY_F4) {
190       Print("\nToggling Speaker Port\n");
191       SerialPrintLevel(100,"\nToggling Speaker Port\n");
192       *doIBuzz = (*doIBuzz + 1) % 2;
193     } else if (key_press == KEY_F5) {
194       Print("\nMachine Restart\n");
195       SerialPrintLevel(100,"\nMachine Restart\n");
196       machine_real_restart();
197     }
198   }
199   return;
200 }
201
202
203
204 extern char BSS_START, BSS_END;
205
206 extern char end;
207
208 /*
209 void VM_Thread(ulong_t arg) 
210 {
211   int ret;
212   struct VMDescriptor *vm = (struct VMDescriptor *) arg;
213
214   SerialPrintLevel(100,"VM_Thread: Launching VM with (entry_ip=%x, exit_eip=%x, guest_esp=%x)\n",
215               vm->entry_ip, vm->exit_eip, vm->guest_esp);
216
217   SerialPrintLevel(100,"VM_Thread: You should see nothing further from me\n");
218
219
220   ret = VMLaunch(vm);
221
222
223   SerialPrintLevel(100,"VM_Thread: uh oh...");
224
225   switch (ret) { 
226   case VMX_SUCCESS:
227     SerialPrintLevel(100,"Normal VMExit Occurred\n");
228     break;
229   case VMX_FAIL_INVALID:
230     SerialPrintLevel(100,"Possibile invalid VMCS (%.8x)\n", ret);
231     break;
232   case VMX_FAIL_VALID:
233     SerialPrintLevel(100,"Valid VMCS, errorcode recorded in VMCS\n");
234     break;
235   case VMM_ERROR:
236     SerialPrintLevel(100,"VMM Error\n");
237     break;
238   default:
239     SerialPrintLevel(100,"VMLaunch returned unknown error (%.8x)\n", ret);
240     break;
241   }
242   
243   SerialPrintLevel(100,"VM_Thread: Spinning\n");
244   while (1) {}
245     
246 }
247 */
248
249 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
250 {
251   uint_t address;
252
253   for (address=start;address<start+length;address+=PAGE_SIZE) { 
254     void *page;
255     pte_t pte = template_pte;
256     
257     page=Alloc_Page();
258     KASSERT(page);
259     
260     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
261
262     KASSERT(MapPage((void*)address,&pte,1));
263   }
264   
265   return 0;
266 }
267     
268
269
270 /*
271  * Kernel C code entry point.
272  * Initializes kernel subsystems, mounts filesystems,
273  * and spawns init process.
274  */
275 void Main(struct Boot_Info* bootInfo)
276 {
277   struct Kernel_Thread * key_thread;
278   struct Kernel_Thread * spkr_thread;
279   // struct Kernel_Thread * vm_thread;
280   // struct VMDescriptor    vm;
281
282   ulong_t doIBuzz = 0;
283
284   Init_BSS();
285   Init_Screen();
286
287
288   InitSerial();
289   Init_Mem(bootInfo);
290   Init_CRC32();
291   Init_TSS();
292   Init_Interrupts();
293   Init_Scheduler();
294   Init_Traps();
295   Init_Timer();
296   Init_Keyboard();
297   Init_VM(bootInfo);
298   Init_Paging();
299
300   //  Init_IDE();
301
302   Print("Done; stalling\n");
303
304
305   
306 #if 0
307   SerialPrint("Dumping VM kernel Code (first 512 bytes @ 0x%x)\n",VM_KERNEL_START);
308   SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
309   /*
310     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
311     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
312   */
313 #endif
314
315 #if 1
316   SerialPrint("Dumping GUEST KERNEL CODE (first 512*2 bytes @ 0x100000)\n");
317   SerialMemDump((unsigned char *)0x100000, 512*2);
318 #endif
319
320
321
322
323   SerialPrint("\n\nHello, Welcome to this horrid output-only serial interface\n");
324   SerialPrint("Eventually, this will let us control the VMM\n\n");
325  
326   SerialPrint("\n\n===>");
327   
328   
329
330   Init_VMM();
331
332   
333   SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
334   
335   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
336   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
337
338
339
340
341
342
343   // Try to launch a real VM
344
345
346   // We now map pages of physical memory into where we are going
347   // to slap the vmxassist, bios, and vgabios code
348   /*
349   pte_t template_pte;
350
351   template_pte.present=1;
352   template_pte.flags=VM_WRITE|VM_READ|VM_USER|VM_EXEC;
353   template_pte.accessed=0;
354   template_pte.dirty=0;
355   template_pte.pteAttribute=0;
356   template_pte.globalPage=0;
357   template_pte.kernelInfo=0;
358   
359   SerialPrintLevel(1000,"Allocating Pages for VM kernel\n");
360   
361 #define SEGLEN (1024*64)
362
363   AllocateAndMapPagesForRange(START_OF_VM+0x100000, VM_KERNEL_LENGTH / 512, template_pte);
364 */
365   // Now we should be copying into actual memory
366
367   //SerialPrintLevel(1000,"Copying VM code from %x to %x (%d bytes)\n", VM_KERNEL_START, START_OF_VM+0x100000,VM_KERNEL_LENGTH);
368   //memcpy((char*)(START_OF_VM+0x100000),(char*)VM_KERNEL_START,VM_KERNEL_LENGTH);
369
370   //SerialPrintLevel(1000, "VM copied\n");
371
372   /*
373   // jump into vmxassist
374   vm.entry_ip=(uint_t)0x00107fd0;
375   vm.exit_eip=0;
376   // Put the stack at 512K
377   vm.guest_esp=(uint_t)4096 + 8192 - 4;
378   *(unsigned int *)(vm.guest_esp) = 1024 * 1024;
379   vm.guest_esp -= 4;
380   *(unsigned int *)(vm.guest_esp) = 8;
381   vm.guest_esp -= 4;
382   *(unsigned int *)(vm.guest_esp) = vm.guest_esp + 4;;
383   vm.guest_esp -= 4;
384   *(unsigned int *)(vm.guest_esp) = vm.entry_ip;
385   //  vm.guest_esp -= 4;
386
387  
388   SerialMemDump((unsigned char *)vm.entry_ip, 512);
389   */
390  
391   // vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
392
393   
394   SerialPrintLevel(1000,"Next: setup GDT\n");
395
396
397
398   TODO("Write a Virtual Machine Monitor");
399   
400   
401   /* Now this thread is done. */
402   Exit(0);
403 }