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/debug.h>
31 #include <geekos/vmm.h>
33 #include <geekos/gdt.h>
35 #include <geekos/vmm_sizes.h>
36 #include <geekos/vmm_stubs.h>
39 static inline unsigned int cpuid_ecx(unsigned int op)
41 unsigned int eax, ecx;
44 : "=a" (eax), "=c" (ecx)
54 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
55 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
56 extern uint_t Get_EIP();
57 extern uint_t Get_ESP();
58 extern uint_t Get_EBP();
63 #define SPEAKER_PORT 0x61
66 void Buzz(unsigned delay, unsigned num)
72 init=In_Byte(SPEAKER_PORT);
75 Out_Byte(SPEAKER_PORT, init|0x2);
76 for (j=0;j<delay;j++) {
79 Out_Byte(SPEAKER_PORT, init);
80 for (j=0;j<delay;j++) {
86 inline void MyOut_Byte(ushort_t port, uchar_t value)
88 __asm__ __volatile__ (
91 : "a" (value), "Nd" (port)
96 * Read a byte from an I/O port.
98 inline uchar_t MyIn_Byte(ushort_t port)
102 __asm__ __volatile__ (
112 extern void MyBuzzVM();
114 #define MYBUZZVM_START MyBuzzVM
115 #define MYBUZZVM_LEN 0x3d
124 SerialPrint("Starting To Buzz\n");
126 init=MyIn_Byte(SPEAKER_PORT);
129 MyOut_Byte(SPEAKER_PORT, init|0x2);
130 for (j=0;j<1000000;j++) {
133 MyOut_Byte(SPEAKER_PORT, init);
134 for (j=0;j<1000000;j++) {
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...
171 void Hello(ulong_t arg)
180 __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
185 void Keyboard_Listener(ulong_t arg) {
186 ulong_t * doIBuzz = (ulong_t*)arg;
189 Print("Press F4 to turn on/off the speaker\n");
191 while ((key_press = Wait_For_Key())) {
192 if (key_press == KEY_F4) {
193 Print("\nToggling Speaker Port\n");
194 SerialPrintLevel(100,"\nToggling Speaker Port\n");
195 *doIBuzz = (*doIBuzz + 1) % 2;
196 } else if (key_press == KEY_F5) {
197 Print("\nMachine Restart\n");
198 SerialPrintLevel(100,"\nMachine Restart\n");
199 machine_real_restart();
207 extern char BSS_START, BSS_END;
212 void VM_Thread(ulong_t arg)
215 struct VMDescriptor *vm = (struct VMDescriptor *) arg;
217 SerialPrintLevel(100,"VM_Thread: Launching VM with (entry_ip=%x, exit_eip=%x, guest_esp=%x)\n",
218 vm->entry_ip, vm->exit_eip, vm->guest_esp);
220 SerialPrintLevel(100,"VM_Thread: You should see nothing further from me\n");
226 SerialPrintLevel(100,"VM_Thread: uh oh...");
230 SerialPrintLevel(100,"Normal VMExit Occurred\n");
232 case VMX_FAIL_INVALID:
233 SerialPrintLevel(100,"Possibile invalid VMCS (%.8x)\n", ret);
236 SerialPrintLevel(100,"Valid VMCS, errorcode recorded in VMCS\n");
239 SerialPrintLevel(100,"VMM Error\n");
242 SerialPrintLevel(100,"VMLaunch returned unknown error (%.8x)\n", ret);
246 SerialPrintLevel(100,"VM_Thread: Spinning\n");
254 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
258 for (address=start;address<start+length;address+=PAGE_SIZE) {
260 pte_t pte = template_pte;
265 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
267 KASSERT(MapPage((void*)address,&pte,1));
276 * Kernel C code entry point.
277 * Initializes kernel subsystems, mounts filesystems,
278 * and spawns init process.
280 void Main(struct Boot_Info* bootInfo)
282 struct Kernel_Thread * key_thread;
283 struct Kernel_Thread * spkr_thread;
284 // struct Kernel_Thread * vm_thread;
285 // struct VMDescriptor vm;
307 Print("Done; stalling\n");
312 SerialPrint("Dumping VM kernel Code (first 512 bytes @ 0x%x)\n",VM_KERNEL_START);
313 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
315 SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
316 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
321 SerialPrint("Dumping GUEST KERNEL CODE (first 512*2 bytes @ 0x100000)\n");
322 SerialMemDump((unsigned char *)0x100000, 512*2);
328 struct vmm_os_hooks os_hooks;
329 struct vmm_ctrl_ops vmm_ops;
330 guest_info_t vm_info;
331 memset(&os_hooks, 0, sizeof(struct vmm_os_hooks));
332 memset(&vmm_ops, 0, sizeof(struct vmm_ctrl_ops));
333 memset(&vm_info, 0, sizeof(guest_info_t));
335 os_hooks.print_debug = &PrintBoth;
336 os_hooks.print_info = &Print;
337 os_hooks.print_trace = &SerialPrint;
338 os_hooks.Allocate_Pages = &Allocate_VMM_Pages;
339 os_hooks.Free_Page = &Free_VMM_Page;
341 Init_VMM(&os_hooks, &vmm_ops);
345 vm_info.rip = (ullong_t)(void*)&BuzzVM;
346 vm_info.rsp = (ulong_t)Alloc_Page();
348 SerialPrint("Initializing Guest\n");
349 (vmm_ops).init_guest(&vm_info);
350 SerialPrint("Starting Guest\n");
351 (vmm_ops).start_guest(&vm_info);
356 SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
358 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
359 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
366 // Try to launch a real VM
369 // We now map pages of physical memory into where we are going
370 // to slap the vmxassist, bios, and vgabios code
374 template_pte.present=1;
375 template_pte.flags=VM_WRITE|VM_READ|VM_USER|VM_EXEC;
376 template_pte.accessed=0;
377 template_pte.dirty=0;
378 template_pte.pteAttribute=0;
379 template_pte.globalPage=0;
380 template_pte.kernelInfo=0;
382 SerialPrintLevel(1000,"Allocating Pages for VM kernel\n");
384 #define SEGLEN (1024*64)
386 AllocateAndMapPagesForRange(START_OF_VM+0x100000, VM_KERNEL_LENGTH / 512, template_pte);
388 // Now we should be copying into actual memory
390 //SerialPrintLevel(1000,"Copying VM code from %x to %x (%d bytes)\n", VM_KERNEL_START, START_OF_VM+0x100000,VM_KERNEL_LENGTH);
391 //memcpy((char*)(START_OF_VM+0x100000),(char*)VM_KERNEL_START,VM_KERNEL_LENGTH);
393 //SerialPrintLevel(1000, "VM copied\n");
396 // jump into vmxassist
397 vm.entry_ip=(uint_t)0x00107fd0;
399 // Put the stack at 512K
400 vm.guest_esp=(uint_t)4096 + 8192 - 4;
401 *(unsigned int *)(vm.guest_esp) = 1024 * 1024;
403 *(unsigned int *)(vm.guest_esp) = 8;
405 *(unsigned int *)(vm.guest_esp) = vm.guest_esp + 4;;
407 *(unsigned int *)(vm.guest_esp) = vm.entry_ip;
408 // vm.guest_esp -= 4;
411 SerialMemDump((unsigned char *)vm.entry_ip, 512);
414 // vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
417 SerialPrintLevel(1000,"Next: setup GDT\n");
421 TODO("Write a Virtual Machine Monitor");
424 /* Now this thread is done. */