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 #define SPEAKER_PORT 0x61
43 void Buzz(unsigned delay, unsigned num)
49 init=In_Byte(SPEAKER_PORT);
52 Out_Byte(SPEAKER_PORT, init|0x2);
53 for (j=0;j<delay;j++) {
56 Out_Byte(SPEAKER_PORT, init);
57 for (j=0;j<delay;j++) {
63 inline void MyOut_Byte(ushort_t port, uchar_t value)
65 __asm__ __volatile__ (
68 : "a" (value), "Nd" (port)
73 * Read a byte from an I/O port.
75 inline uchar_t MyIn_Byte(ushort_t port)
79 __asm__ __volatile__ (
90 int IO_Read(ushort_t port, void * dst, uint_t length) {
94 for (i = 0; i < length; i++) {
95 *iter = MyIn_Byte(port);
104 int IO_Write(ushort_t port, void * src, uint_t length) {
105 uchar_t * iter = src;
109 for (i = 0; i < length; i++) {
110 MyOut_Byte(port, *iter);
125 __asm__ __volatile__ (
131 PrintBoth("Starting To Buzz\n");
133 init=MyIn_Byte(SPEAKER_PORT);
136 MyOut_Byte(SPEAKER_PORT, init|0x2);
137 for (j=0;j<1000000;j++) {
140 MyOut_Byte(SPEAKER_PORT, init);
141 for (j=0;j<1000000;j++) {
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...
170 void Keyboard_Listener(ulong_t arg) {
171 ulong_t * doIBuzz = (ulong_t*)arg;
174 Print("Press F4 to turn on/off the speaker\n");
176 while ((key_press = Wait_For_Key())) {
177 if (key_press == KEY_F4) {
178 Print("\nToggling Speaker Port\n");
179 SerialPrintLevel(100,"\nToggling Speaker Port\n");
180 *doIBuzz = (*doIBuzz + 1) % 2;
181 } else if (key_press == KEY_F5) {
182 Print("\nMachine Restart\n");
183 SerialPrintLevel(100,"\nMachine Restart\n");
184 machine_real_restart();
192 extern char BSS_START, BSS_END;
197 /* This is an ugly hack to get at the VM memory */
198 ulong_t vm_range_start;
199 ulong_t vm_range_end;
200 ulong_t guest_kernel_start;
201 ulong_t guest_kernel_end;
205 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
209 for (address=start;address<start+length;address+=PAGE_SIZE) {
211 pte_t pte = template_pte;
216 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
218 KASSERT(MapPage((void*)address,&pte,1));
227 * Kernel C code entry point.
228 * Initializes kernel subsystems, mounts filesystems,
229 * and spawns init process.
231 void Main(struct Boot_Info* bootInfo)
233 struct Kernel_Thread * key_thread;
234 struct Kernel_Thread * spkr_thread;
256 // Print("Done; stalling\n");
261 SerialPrint("Dumping VM kernel Code (first 128 bytes @ 0x%x)\n", 0x100000);
262 SerialMemDump((unsigned char *)0xfe000, 4096);
264 SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
265 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
270 SerialPrint("Dumping BIOS code f0000-fffff\n\n");
271 SerialMemDump((unsigned char *)0xf0000, 65536);
273 SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
274 SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
279 SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
280 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
281 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
286 struct vmm_os_hooks os_hooks;
287 struct vmm_ctrl_ops vmm_ops;
288 guest_info_t vm_info;
292 memset(&os_hooks, 0, sizeof(struct vmm_os_hooks));
293 memset(&vmm_ops, 0, sizeof(struct vmm_ctrl_ops));
294 memset(&vm_info, 0, sizeof(guest_info_t));
296 os_hooks.print_debug = &PrintBoth;
297 os_hooks.print_info = &Print;
298 os_hooks.print_trace = &SerialPrint;
299 os_hooks.allocate_pages = &Allocate_VMM_Pages;
300 os_hooks.free_page = &Free_VMM_Page;
301 os_hooks.malloc = &VMM_Malloc;
302 os_hooks.free = &VMM_Free;
303 os_hooks.vaddr_to_paddr = &Identity;
304 os_hooks.paddr_to_vaddr = &Identity;
308 Init_VMM(&os_hooks, &vmm_ops);
310 init_shadow_map(&(vm_info.mem_map));
311 init_shadow_page_state(&(vm_info.shadow_page_state));
312 vm_info.page_mode = SHADOW_PAGING;
314 init_vmm_io_map(&(vm_info.io_map));
319 // add_shared_mem_range(&(vm_info.mem_layout), 0, 0x800000, 0x10000);
320 // add_shared_mem_range(&(vm_info.mem_layout), 0, 0x1000000, 0);
322 rip = (ulong_t)(void*)&BuzzVM;
324 // rip = (addr_t)(void*)&exit_test;
327 rsp = (addr_t)Alloc_Page();
329 vm_info.rsp = (rsp +4092 );// - 0x2000;
333 //add_shared_mem_range(&(vm_info.mem_layout), 0x0, 0x1000, 0x100000);
334 // add_shared_mem_range(&(vm_info.mem_layout), 0x0, 0x100000, 0x0);
336 shadow_region_t *ent = Malloc(sizeof(shadow_region_t));;
337 init_shadow_region_physical(ent,0,0x100000,GUEST_REGION_PHYSICAL_MEMORY,
338 0x100000, HOST_REGION_PHYSICAL_MEMORY);
339 add_shadow_region(&(vm_info.mem_map),ent);
341 hook_io_port(&(vm_info.io_map), 0x61, &IO_Read, &IO_Write);
344 vm_info.cs.base=0xf000;
345 vm_info.cs.limit=0xffff;
347 vm_info.rip = 0xfff0;
351 PrintBoth("Initializing Guest (eip=0x%.8x) (esp=0x%.8x)\n", (uint_t)vm_info.rip,(uint_t)vm_info.rsp);
352 (vmm_ops).init_guest(&vm_info);
353 PrintBoth("Starting Guest\n");
354 (vmm_ops).start_guest(&vm_info);
361 TODO("Write a Virtual Machine Monitor");