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>
8 * This is free software. You are permitted to use,
9 * redistribute, and modify it as specified in the file "COPYING".
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/vmx.h>
29 #include <geekos/vmcs.h>
31 #include <geekos/gdt.h>
33 #include <geekos/vmm_sizes.h>
36 static inline unsigned int cpuid_ecx(unsigned int op)
38 unsigned int eax, ecx;
41 : "=a" (eax), "=c" (ecx)
51 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
52 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
53 extern uint_t Get_EIP();
54 extern uint_t Get_ESP();
55 extern uint_t Get_EBP();
60 #define SPEAKER_PORT 0x61
63 void Buzz(unsigned delay, unsigned num)
69 init=In_Byte(SPEAKER_PORT);
72 Out_Byte(SPEAKER_PORT, init|0x2);
73 for (j=0;j<delay;j++) {
76 Out_Byte(SPEAKER_PORT, init);
77 for (j=0;j<delay;j++) {
83 inline void MyOut_Byte(ushort_t port, uchar_t value)
85 __asm__ __volatile__ (
88 : "a" (value), "Nd" (port)
93 * Read a byte from an I/O port.
95 inline uchar_t MyIn_Byte(ushort_t port)
99 __asm__ __volatile__ (
109 extern void MyBuzzVM();
111 #define MYBUZZVM_START MyBuzzVM
112 #define MYBUZZVM_LEN 0x3d
121 init=MyIn_Byte(SPEAKER_PORT);
124 MyOut_Byte(SPEAKER_PORT, init|0x2);
125 for (j=0;j<1000000;j++) {
128 MyOut_Byte(SPEAKER_PORT, init);
129 for (j=0;j<1000000;j++) {
148 extern uint_t VMCS_STORE();
149 extern uint_t VMCS_READ();
153 void Buzzer(ulong_t arg) {
154 ulong_t *doIBuzz = (ulong_t*)arg;
156 // Quick and dirty hack to save my hearing...
157 // I'm not too worried about timing, so I'll deal with concurrency later...
167 void Hello(ulong_t arg)
176 __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
181 void Keyboard_Listener(ulong_t arg) {
182 ulong_t * doIBuzz = (ulong_t*)arg;
185 Print("Press F4 to turn on/off the speaker\n");
187 while ((key_press = Wait_For_Key())) {
188 if (key_press == KEY_F4) {
189 Print("\nToggling Speaker Port\n");
190 SerialPrintLevel(100,"\nToggling Speaker Port\n");
191 *doIBuzz = (*doIBuzz + 1) % 2;
192 } else if (key_press == KEY_F5) {
193 Print("\nMachine Restart\n");
194 SerialPrintLevel(100,"\nMachine Restart\n");
195 machine_real_restart();
203 extern char BSS_START, BSS_END;
208 void VM_Thread(ulong_t arg)
211 struct VMDescriptor *vm = (struct VMDescriptor *) arg;
213 SerialPrintLevel(100,"VM_Thread: Launching VM with (entry_ip=%x, exit_eip=%x, guest_esp=%x)\n",
214 vm->entry_ip, vm->exit_eip, vm->guest_esp);
216 SerialPrintLevel(100,"VM_Thread: You should see nothing further from me\n");
222 SerialPrintLevel(100,"VM_Thread: uh oh...");
226 SerialPrintLevel(100,"Normal VMExit Occurred\n");
228 case VMX_FAIL_INVALID:
229 SerialPrintLevel(100,"Possibile invalid VMCS (%.8x)\n", ret);
232 SerialPrintLevel(100,"Valid VMCS, errorcode recorded in VMCS\n");
235 SerialPrintLevel(100,"VMM Error\n");
238 SerialPrintLevel(100,"VMLaunch returned unknown error (%.8x)\n", ret);
242 SerialPrintLevel(100,"VM_Thread: Spinning\n");
248 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
252 for (address=start;address<start+length;address+=PAGE_SIZE) {
254 pte_t pte = template_pte;
259 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
261 KASSERT(MapPage((void*)address,&pte,1));
270 * Kernel C code entry point.
271 * Initializes kernel subsystems, mounts filesystems,
272 * and spawns init process.
274 void Main(struct Boot_Info* bootInfo)
276 struct Kernel_Thread * key_thread;
277 struct Kernel_Thread * spkr_thread;
278 struct Kernel_Thread * vm_thread;
279 struct VMDescriptor vm;
305 SerialPrint("Dumping VMXASSIST Code (first 512 bytes @ 0x%x)\n",VMXASSIST_START);
306 SerialMemDump((unsigned char *)VMXASSIST_START, 512);
307 SerialPrint("Dumping ROMBIOS Code (first 512 bytes @ 0x%x)\n",BIOS_START);
308 SerialMemDump((unsigned char *)BIOS_START, 512);
309 SerialPrint("Dumping ROMBIOS Code (Second copy) (first 512 bytes @ 0x%x)\n",BIOS2_START);
310 SerialMemDump((unsigned char *)BIOS2_START, 512);
311 SerialPrint("Dumping VGABIOS Code (first 512 bytes @ 0x%x)\n",VGA_BIOS_START);
312 SerialMemDump((unsigned char *)VGA_BIOS_START, 512);
318 SerialPrint("\n\nHello, Welcome to this horrid output-only serial interface\n");
319 SerialPrint("Eventually, this will let us control the VMM\n\n");
321 SerialPrint("\n\n===>");
324 SerialPrintLevel(100,"Initializing VMX\n");
325 PrintBoth("Initializing VMX\n");
326 VmxOnRegion * vmxRegion = InitVMX();
328 if (vmxRegion==NULL) {
329 PrintBoth("VMX Cannot be turned on. Halted.\n");
335 SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
337 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
338 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
341 // Enable this to run the simple buzzer VM
344 // Put the entry around 0x10000, where the geekos kernel used to live
345 vm.entry_ip=(uint_t)0x10000;
347 // Put the stack as the last thing in the VM partition
348 vm.guest_esp=(uint_t)START_OF_VM+VM_SIZE-1;
351 memcpy(vm.entry_ip,MYBUZZVM_START,MYBUZZVM_LEN);
353 SerialPrintLevel(1000,"VM-Launching MyBuzzVM after copy to 0x10000\n");
355 vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
361 // write the hello VM down to where we would usually put
362 // vmxassist, and see if it can talk to us
363 vm.entry_ip=(uint_t)START_OF_VM+0xd000000;
365 // Put the stack as the last thing in the VM partition
366 vm.guest_esp=(uint_t)START_OF_VM+VM_SIZE-1;
369 memcpy((void*)(vm.entry_ip),Hello,200); // 200 should be plenty
371 SerialPrintLevel(1000,"VM-Launching HelloVM after copy to 0xd000000\n");
373 vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
376 // Try to launch a real VM
378 // First we will copy down VMXAssist, then we'll launch that
379 // and see if it can handle the system bios
381 // We now map pages of physical memory into where we are going
382 // to slap the vmxassist, bios, and vgabios code
385 template_pte.present=1;
386 template_pte.flags=VM_WRITE|VM_READ|VM_USER|VM_EXEC;
387 template_pte.accessed=0;
388 template_pte.dirty=0;
389 template_pte.pteAttribute=0;
390 template_pte.globalPage=0;
391 template_pte.kernelInfo=0;
393 SerialPrintLevel(1000,"Allocating Pages for VMXASSIST, BIOS, and VGA BIOS\n");
395 #define SEGLEN (1024*64)
397 AllocateAndMapPagesForRange(START_OF_VM+0xd0000, SEGLEN, template_pte);
398 AllocateAndMapPagesForRange(START_OF_VM+0xf0000, SEGLEN, template_pte);
399 AllocateAndMapPagesForRange(START_OF_VM+0xc0000, SEGLEN, template_pte);
401 // Now we should be copying into actual memory
403 SerialPrintLevel(1000,"Copying VMXASSIST code from %x to %x (%d bytes)\n", VMXASSIST_START, START_OF_VM+0xd0000,VMXASSIST_LENGTH);
404 memcpy((char*)(START_OF_VM+0xd0000),(char*)VMXASSIST_START,VMXASSIST_LENGTH);
405 SerialPrintLevel(1000,"Copying BIOS (2nd copy) code from %x to %x (%d bytes)\n", BIOS2_START, START_OF_VM+0xf0000,BIOS_LENGTH);
406 memcpy((char*)(START_OF_VM+0xf0000),(char*)BIOS2_START,BIOS_LENGTH);
407 SerialPrintLevel(1000,"Copying VGA BIOS code from %x to %x (%d bytes)\n", VGA_BIOS_START, START_OF_VM+0xc0000,VGA_BIOS_LENGTH);
408 memcpy((char *)(START_OF_VM+0xc0000),(char*)VGA_BIOS_START,VGA_BIOS_LENGTH);
410 // jump into vmxassist
411 vm.entry_ip=(uint_t)0xd0000;
413 // Put the stack at 512K
414 vm.guest_esp=(uint_t)START_OF_VM+1024*512;
416 SerialPrintLevel(1000,"VM-Launching to vmxassist for boot\n");
418 vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
421 SerialPrintLevel(1000,"Next: setup GDT\n");
427 TODO("Write a Virtual Machine Monitor");
430 /* Now this thread is done. */