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