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/ide.h>
30 #include <geekos/vmx.h>
31 #include <geekos/vmcs.h>
33 #include <geekos/gdt.h>
35 #include <geekos/vmm_sizes.h>
38 static inline unsigned int cpuid_ecx(unsigned int op)
40 unsigned int eax, ecx;
43 : "=a" (eax), "=c" (ecx)
53 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
54 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
55 extern uint_t Get_EIP();
56 extern uint_t Get_ESP();
57 extern uint_t Get_EBP();
62 #define SPEAKER_PORT 0x61
65 void Buzz(unsigned delay, unsigned num)
71 init=In_Byte(SPEAKER_PORT);
74 Out_Byte(SPEAKER_PORT, init|0x2);
75 for (j=0;j<delay;j++) {
78 Out_Byte(SPEAKER_PORT, init);
79 for (j=0;j<delay;j++) {
85 inline void MyOut_Byte(ushort_t port, uchar_t value)
87 __asm__ __volatile__ (
90 : "a" (value), "Nd" (port)
95 * Read a byte from an I/O port.
97 inline uchar_t MyIn_Byte(ushort_t port)
101 __asm__ __volatile__ (
111 extern void MyBuzzVM();
113 #define MYBUZZVM_START MyBuzzVM
114 #define MYBUZZVM_LEN 0x3d
123 init=MyIn_Byte(SPEAKER_PORT);
126 MyOut_Byte(SPEAKER_PORT, init|0x2);
127 for (j=0;j<1000000;j++) {
130 MyOut_Byte(SPEAKER_PORT, init);
131 for (j=0;j<1000000;j++) {
150 extern uint_t VMCS_STORE();
151 extern uint_t VMCS_READ();
155 void Buzzer(ulong_t arg) {
156 ulong_t *doIBuzz = (ulong_t*)arg;
158 // Quick and dirty hack to save my hearing...
159 // I'm not too worried about timing, so I'll deal with concurrency later...
169 void Hello(ulong_t arg)
178 __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
183 void Keyboard_Listener(ulong_t arg) {
184 ulong_t * doIBuzz = (ulong_t*)arg;
187 Print("Press F4 to turn on/off the speaker\n");
189 while ((key_press = Wait_For_Key())) {
190 if (key_press == KEY_F4) {
191 Print("\nToggling Speaker Port\n");
192 SerialPrintLevel(100,"\nToggling Speaker Port\n");
193 *doIBuzz = (*doIBuzz + 1) % 2;
194 } else if (key_press == KEY_F5) {
195 Print("\nMachine Restart\n");
196 SerialPrintLevel(100,"\nMachine Restart\n");
197 machine_real_restart();
205 extern char BSS_START, BSS_END;
210 void VM_Thread(ulong_t arg)
213 struct VMDescriptor *vm = (struct VMDescriptor *) arg;
215 SerialPrintLevel(100,"VM_Thread: Launching VM with (entry_ip=%x, exit_eip=%x, guest_esp=%x)\n",
216 vm->entry_ip, vm->exit_eip, vm->guest_esp);
218 SerialPrintLevel(100,"VM_Thread: You should see nothing further from me\n");
224 SerialPrintLevel(100,"VM_Thread: uh oh...");
228 SerialPrintLevel(100,"Normal VMExit Occurred\n");
230 case VMX_FAIL_INVALID:
231 SerialPrintLevel(100,"Possibile invalid VMCS (%.8x)\n", ret);
234 SerialPrintLevel(100,"Valid VMCS, errorcode recorded in VMCS\n");
237 SerialPrintLevel(100,"VMM Error\n");
240 SerialPrintLevel(100,"VMLaunch returned unknown error (%.8x)\n", ret);
244 SerialPrintLevel(100,"VM_Thread: Spinning\n");
250 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
254 for (address=start;address<start+length;address+=PAGE_SIZE) {
256 pte_t pte = template_pte;
261 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
263 KASSERT(MapPage((void*)address,&pte,1));
272 * Kernel C code entry point.
273 * Initializes kernel subsystems, mounts filesystems,
274 * and spawns init process.
276 void Main(struct Boot_Info* bootInfo)
278 struct Kernel_Thread * key_thread;
279 struct Kernel_Thread * spkr_thread;
280 struct Kernel_Thread * vm_thread;
281 struct VMDescriptor vm;
303 Print("Done; stalling\n");
310 SerialPrint("Dumping VM kernel Code (first 512 bytes @ 0x%x)\n",VM_KERNEL_START);
311 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
313 SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
314 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
320 SerialPrint("\n\nHello, Welcome to this horrid output-only serial interface\n");
321 SerialPrint("Eventually, this will let us control the VMM\n\n");
323 SerialPrint("\n\n===>");
326 SerialPrintLevel(100,"Initializing VMX\n");
327 PrintBoth("Initializing VMX\n");
328 VmxOnRegion * vmxRegion = InitVMX();
330 if (vmxRegion==NULL) {
331 PrintBoth("VMX Cannot be turned on. Halted.\n");
337 SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
339 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
340 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
343 // Enable this to run the simple buzzer VM
346 // Put the entry around 0x10000, where the geekos kernel used to live
347 vm.entry_ip=(uint_t)0x10000;
349 // Put the stack as the last thing in the VM partition
350 vm.guest_esp=(uint_t)START_OF_VM+VM_SIZE-1;
353 memcpy(vm.entry_ip,MYBUZZVM_START,MYBUZZVM_LEN);
355 SerialPrintLevel(1000,"VM-Launching MyBuzzVM after copy to 0x10000\n");
357 vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
363 // write the hello VM down to where we would usually put
364 // vmxassist, and see if it can talk to us
365 vm.entry_ip=(uint_t)START_OF_VM+0xd000000;
367 // Put the stack as the last thing in the VM partition
368 vm.guest_esp=(uint_t)START_OF_VM+VM_SIZE-1;
371 memcpy((void*)(vm.entry_ip),Hello,200); // 200 should be plenty
373 SerialPrintLevel(1000,"VM-Launching HelloVM after copy to 0xd000000\n");
375 vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
378 // Try to launch a real VM
380 // First we will copy down VMXAssist, then we'll launch that
381 // and see if it can handle the system bios
383 // We now map pages of physical memory into where we are going
384 // to slap the vmxassist, bios, and vgabios code
387 template_pte.present=1;
388 template_pte.flags=VM_WRITE|VM_READ|VM_USER|VM_EXEC;
389 template_pte.accessed=0;
390 template_pte.dirty=0;
391 template_pte.pteAttribute=0;
392 template_pte.globalPage=0;
393 template_pte.kernelInfo=0;
395 SerialPrintLevel(1000,"Allocating Pages for VM kernel\n");
397 #define SEGLEN (1024*64)
399 AllocateAndMapPagesForRange(START_OF_VM+0x100000, VM_KERNEL_LENGTH / 512, template_pte);
401 // Now we should be copying into actual memory
403 SerialPrintLevel(1000,"Copying VM code from %x to %x (%d bytes)\n", VM_KERNEL_START, START_OF_VM+0x100000,VM_KERNEL_LENGTH);
404 memcpy((char*)(START_OF_VM+0x100000),(char*)VM_KERNEL_START,VM_KERNEL_LENGTH);
406 //SerialPrintLevel(1000, "VM copied\n");
408 // jump into vmxassist
409 vm.entry_ip=(uint_t)0x100000;
411 // Put the stack at 512K
412 vm.guest_esp=(uint_t)START_OF_VM+1024*512;
414 SerialPrintLevel(1000,"VM-Launching to vmxassist for boot\n");
416 vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
419 SerialPrintLevel(1000,"Next: setup GDT\n");
425 TODO("Write a Virtual Machine Monitor");
428 /* Now this thread is done. */