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>
29 #include <geekos/malloc.h>
31 #include <geekos/debug.h>
32 #include <geekos/vmm.h>
34 #include <geekos/gdt.h>
37 #include <geekos/vmm_stubs.h>
40 static inline unsigned int cpuid_ecx(unsigned int op)
42 unsigned int eax, ecx;
45 : "=a" (eax), "=c" (ecx)
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();
64 #define SPEAKER_PORT 0x61
67 void Buzz(unsigned delay, unsigned num)
73 init=In_Byte(SPEAKER_PORT);
76 Out_Byte(SPEAKER_PORT, init|0x2);
77 for (j=0;j<delay;j++) {
80 Out_Byte(SPEAKER_PORT, init);
81 for (j=0;j<delay;j++) {
87 inline void MyOut_Byte(ushort_t port, uchar_t value)
89 __asm__ __volatile__ (
92 : "a" (value), "Nd" (port)
97 * Read a byte from an I/O port.
99 inline uchar_t MyIn_Byte(ushort_t port)
103 __asm__ __volatile__ (
113 extern void MyBuzzVM();
115 #define MYBUZZVM_START MyBuzzVM
116 #define MYBUZZVM_LEN 0x3d
125 SerialPrint("Starting To Buzz\n");
127 init=MyIn_Byte(SPEAKER_PORT);
130 MyOut_Byte(SPEAKER_PORT, init|0x2);
131 for (j=0;j<1000000;j++) {
134 MyOut_Byte(SPEAKER_PORT, init);
135 for (j=0;j<1000000;j++) {
156 void Buzzer(ulong_t arg) {
157 ulong_t *doIBuzz = (ulong_t*)arg;
159 // Quick and dirty hack to save my hearing...
160 // I'm not too worried about timing, so I'll deal with concurrency later...
172 void Hello(ulong_t arg)
181 __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
186 void Keyboard_Listener(ulong_t arg) {
187 ulong_t * doIBuzz = (ulong_t*)arg;
190 Print("Press F4 to turn on/off the speaker\n");
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();
208 extern char BSS_START, BSS_END;
213 void VM_Thread(ulong_t arg)
216 struct VMDescriptor *vm = (struct VMDescriptor *) arg;
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);
221 SerialPrintLevel(100,"VM_Thread: You should see nothing further from me\n");
227 SerialPrintLevel(100,"VM_Thread: uh oh...");
231 SerialPrintLevel(100,"Normal VMExit Occurred\n");
233 case VMX_FAIL_INVALID:
234 SerialPrintLevel(100,"Possibile invalid VMCS (%.8x)\n", ret);
237 SerialPrintLevel(100,"Valid VMCS, errorcode recorded in VMCS\n");
240 SerialPrintLevel(100,"VMM Error\n");
243 SerialPrintLevel(100,"VMLaunch returned unknown error (%.8x)\n", ret);
247 SerialPrintLevel(100,"VM_Thread: Spinning\n");
255 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
259 for (address=start;address<start+length;address+=PAGE_SIZE) {
261 pte_t pte = template_pte;
266 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
268 KASSERT(MapPage((void*)address,&pte,1));
277 * Kernel C code entry point.
278 * Initializes kernel subsystems, mounts filesystems,
279 * and spawns init process.
281 void Main(struct Boot_Info* bootInfo)
283 struct Kernel_Thread * key_thread;
284 struct Kernel_Thread * spkr_thread;
285 // struct Kernel_Thread * vm_thread;
286 // struct VMDescriptor vm;
308 Print("Done; stalling\n");
313 SerialPrint("Dumping VM kernel Code (first 512 bytes @ 0x%x)\n",VM_KERNEL_START);
314 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
316 SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
317 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
322 SerialPrint("Dumping GUEST KERNEL CODE (first 512*2 bytes @ 0x100000)\n");
323 SerialMemDump((unsigned char *)0x100000, 512*2);
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));
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;
345 Init_VMM(&os_hooks, &vmm_ops);
348 init_mem_layout(&(vm_info.mem_layout));
349 init_mem_list(&(vm_info.mem_list));
351 // add_mem_list_pages(&(vm_info.mem_list), START_OF_VM, 20);
352 //add_guest_mem_range(&(vm_info.mem_layout), 0, 20);
355 vm_info.rip = (ullong_t)(void*)&BuzzVM;
356 vm_info.rsp = (ulong_t)Alloc_Page();
358 SerialPrint("Initializing Guest\n");
359 (vmm_ops).init_guest(&vm_info);
360 SerialPrint("Starting Guest\n");
361 (vmm_ops).start_guest(&vm_info);
366 SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
368 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
369 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
376 // Try to launch a real VM
379 // We now map pages of physical memory into where we are going
380 // to slap the vmxassist, bios, and vgabios code
384 template_pte.present=1;
385 template_pte.flags=VM_WRITE|VM_READ|VM_USER|VM_EXEC;
386 template_pte.accessed=0;
387 template_pte.dirty=0;
388 template_pte.pteAttribute=0;
389 template_pte.globalPage=0;
390 template_pte.kernelInfo=0;
392 SerialPrintLevel(1000,"Allocating Pages for VM kernel\n");
394 #define SEGLEN (1024*64)
396 AllocateAndMapPagesForRange(START_OF_VM+0x100000, VM_KERNEL_LENGTH / 512, template_pte);
398 // Now we should be copying into actual memory
400 //SerialPrintLevel(1000,"Copying VM code from %x to %x (%d bytes)\n", VM_KERNEL_START, START_OF_VM+0x100000,VM_KERNEL_LENGTH);
401 //memcpy((char*)(START_OF_VM+0x100000),(char*)VM_KERNEL_START,VM_KERNEL_LENGTH);
403 //SerialPrintLevel(1000, "VM copied\n");
406 // jump into vmxassist
407 vm.entry_ip=(uint_t)0x00107fd0;
409 // Put the stack at 512K
410 vm.guest_esp=(uint_t)4096 + 8192 - 4;
411 *(unsigned int *)(vm.guest_esp) = 1024 * 1024;
413 *(unsigned int *)(vm.guest_esp) = 8;
415 *(unsigned int *)(vm.guest_esp) = vm.guest_esp + 4;;
417 *(unsigned int *)(vm.guest_esp) = vm.entry_ip;
418 // vm.guest_esp -= 4;
421 SerialMemDump((unsigned char *)vm.entry_ip, 512);
424 // vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
427 SerialPrintLevel(1000,"Next: setup GDT\n");
431 TODO("Write a Virtual Machine Monitor");
434 /* Now this thread is done. */