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/vmm.h>
32 #include <geekos/gdt.h>
34 #include <geekos/vmm_sizes.h>
37 static inline unsigned int cpuid_ecx(unsigned int op)
39 unsigned int eax, ecx;
42 : "=a" (eax), "=c" (ecx)
52 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
53 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
54 extern uint_t Get_EIP();
55 extern uint_t Get_ESP();
56 extern uint_t Get_EBP();
61 #define SPEAKER_PORT 0x61
64 void Buzz(unsigned delay, unsigned num)
70 init=In_Byte(SPEAKER_PORT);
73 Out_Byte(SPEAKER_PORT, init|0x2);
74 for (j=0;j<delay;j++) {
77 Out_Byte(SPEAKER_PORT, init);
78 for (j=0;j<delay;j++) {
84 inline void MyOut_Byte(ushort_t port, uchar_t value)
86 __asm__ __volatile__ (
89 : "a" (value), "Nd" (port)
94 * Read a byte from an I/O port.
96 inline uchar_t MyIn_Byte(ushort_t port)
100 __asm__ __volatile__ (
110 extern void MyBuzzVM();
112 #define MYBUZZVM_START MyBuzzVM
113 #define MYBUZZVM_LEN 0x3d
122 init=MyIn_Byte(SPEAKER_PORT);
125 MyOut_Byte(SPEAKER_PORT, init|0x2);
126 for (j=0;j<1000000;j++) {
129 MyOut_Byte(SPEAKER_PORT, init);
130 for (j=0;j<1000000;j++) {
149 extern uint_t VMCS_STORE();
150 extern uint_t VMCS_READ();
154 void Buzzer(ulong_t arg) {
155 ulong_t *doIBuzz = (ulong_t*)arg;
157 // Quick and dirty hack to save my hearing...
158 // I'm not too worried about timing, so I'll deal with concurrency later...
168 void Hello(ulong_t arg)
177 __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
182 void Keyboard_Listener(ulong_t arg) {
183 ulong_t * doIBuzz = (ulong_t*)arg;
186 Print("Press F4 to turn on/off the speaker\n");
188 while ((key_press = Wait_For_Key())) {
189 if (key_press == KEY_F4) {
190 Print("\nToggling Speaker Port\n");
191 SerialPrintLevel(100,"\nToggling Speaker Port\n");
192 *doIBuzz = (*doIBuzz + 1) % 2;
193 } else if (key_press == KEY_F5) {
194 Print("\nMachine Restart\n");
195 SerialPrintLevel(100,"\nMachine Restart\n");
196 machine_real_restart();
204 extern char BSS_START, BSS_END;
209 void VM_Thread(ulong_t arg)
212 struct VMDescriptor *vm = (struct VMDescriptor *) arg;
214 SerialPrintLevel(100,"VM_Thread: Launching VM with (entry_ip=%x, exit_eip=%x, guest_esp=%x)\n",
215 vm->entry_ip, vm->exit_eip, vm->guest_esp);
217 SerialPrintLevel(100,"VM_Thread: You should see nothing further from me\n");
223 SerialPrintLevel(100,"VM_Thread: uh oh...");
227 SerialPrintLevel(100,"Normal VMExit Occurred\n");
229 case VMX_FAIL_INVALID:
230 SerialPrintLevel(100,"Possibile invalid VMCS (%.8x)\n", ret);
233 SerialPrintLevel(100,"Valid VMCS, errorcode recorded in VMCS\n");
236 SerialPrintLevel(100,"VMM Error\n");
239 SerialPrintLevel(100,"VMLaunch returned unknown error (%.8x)\n", ret);
243 SerialPrintLevel(100,"VM_Thread: Spinning\n");
249 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
253 for (address=start;address<start+length;address+=PAGE_SIZE) {
255 pte_t pte = template_pte;
260 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
262 KASSERT(MapPage((void*)address,&pte,1));
271 * Kernel C code entry point.
272 * Initializes kernel subsystems, mounts filesystems,
273 * and spawns init process.
275 void Main(struct Boot_Info* bootInfo)
277 struct Kernel_Thread * key_thread;
278 struct Kernel_Thread * spkr_thread;
279 // struct Kernel_Thread * vm_thread;
280 // struct VMDescriptor vm;
302 Print("Done; stalling\n");
307 SerialPrint("Dumping VM kernel Code (first 512 bytes @ 0x%x)\n",VM_KERNEL_START);
308 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
310 SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
311 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
316 SerialPrint("Dumping GUEST KERNEL CODE (first 512*2 bytes @ 0x100000)\n");
317 SerialMemDump((unsigned char *)0x100000, 512*2);
323 SerialPrint("\n\nHello, Welcome to this horrid output-only serial interface\n");
324 SerialPrint("Eventually, this will let us control the VMM\n\n");
326 SerialPrint("\n\n===>");
333 SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
335 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
336 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
343 // Try to launch a real VM
346 // We now map pages of physical memory into where we are going
347 // to slap the vmxassist, bios, and vgabios code
351 template_pte.present=1;
352 template_pte.flags=VM_WRITE|VM_READ|VM_USER|VM_EXEC;
353 template_pte.accessed=0;
354 template_pte.dirty=0;
355 template_pte.pteAttribute=0;
356 template_pte.globalPage=0;
357 template_pte.kernelInfo=0;
359 SerialPrintLevel(1000,"Allocating Pages for VM kernel\n");
361 #define SEGLEN (1024*64)
363 AllocateAndMapPagesForRange(START_OF_VM+0x100000, VM_KERNEL_LENGTH / 512, template_pte);
365 // Now we should be copying into actual memory
367 //SerialPrintLevel(1000,"Copying VM code from %x to %x (%d bytes)\n", VM_KERNEL_START, START_OF_VM+0x100000,VM_KERNEL_LENGTH);
368 //memcpy((char*)(START_OF_VM+0x100000),(char*)VM_KERNEL_START,VM_KERNEL_LENGTH);
370 //SerialPrintLevel(1000, "VM copied\n");
373 // jump into vmxassist
374 vm.entry_ip=(uint_t)0x00107fd0;
376 // Put the stack at 512K
377 vm.guest_esp=(uint_t)4096 + 8192 - 4;
378 *(unsigned int *)(vm.guest_esp) = 1024 * 1024;
380 *(unsigned int *)(vm.guest_esp) = 8;
382 *(unsigned int *)(vm.guest_esp) = vm.guest_esp + 4;;
384 *(unsigned int *)(vm.guest_esp) = vm.entry_ip;
385 // vm.guest_esp -= 4;
388 SerialMemDump((unsigned char *)vm.entry_ip, 512);
391 // vm_thread = Start_Kernel_Thread(VM_Thread, (ulong_t)&vm,PRIORITY_NORMAL,false);
394 SerialPrintLevel(1000,"Next: setup GDT\n");
398 TODO("Write a Virtual Machine Monitor");
401 /* Now this thread is done. */