Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


a66c42f2af9f991be53f7fefdecc2862dc1c04e8
[palacios.git] / palacios / src / geekos / main.c
1 /*
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>
6  * $Revision: 1.33 $
7  * 
8  * This is free software.  You are permitted to use,
9  * redistribute, and modify it as specified in the file "COPYING".
10  */
11
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>
30
31 #include <geekos/debug.h>
32
33
34 #include <geekos/vm.h>
35 #include <geekos/gdt.h>
36
37
38
39
40 #define SPEAKER_PORT 0x61
41
42
43
44 void Buzz(unsigned delay, unsigned num)
45 {
46   volatile int x;
47   int i,j;
48   unsigned char init;
49   
50   init=In_Byte(SPEAKER_PORT);
51
52   for (i=0;i<num;i++) { 
53     Out_Byte(SPEAKER_PORT, init|0x2);
54     for (j=0;j<delay;j++) { 
55       x+=j;
56     }
57     Out_Byte(SPEAKER_PORT, init);
58     for (j=0;j<delay;j++) { 
59       x+=j;
60     }
61   }
62 }
63
64 inline void MyOut_Byte(ushort_t port, uchar_t value)
65 {
66     __asm__ __volatile__ (
67         "outb %b0, %w1"
68         :
69         : "a" (value), "Nd" (port)
70     );
71 }
72
73 /*
74  * Read a byte from an I/O port.
75  */
76 inline uchar_t MyIn_Byte(ushort_t port)
77 {
78     uchar_t value;
79
80     __asm__ __volatile__ (
81         "inb %w1, %b0"
82         : "=a" (value)
83         : "Nd" (port)
84     );
85
86     return value;
87 }
88
89
90
91
92
93
94
95 void Buzzer(ulong_t arg) {
96   ulong_t *doIBuzz = (ulong_t*)arg;
97   while (1) {
98     // Quick and dirty hack to save my hearing...
99     // I'm not too worried about timing, so I'll deal with concurrency later...
100     if (*doIBuzz == 1) {
101       Buzz(1000000, 10);
102     }
103   }
104
105 }
106
107
108
109
110
111 void Keyboard_Listener(ulong_t arg) {
112   ulong_t * doIBuzz = (ulong_t*)arg;
113   Keycode key_press;
114
115   Print("Press F4 to turn on/off the speaker\n");
116
117   while ((key_press = Wait_For_Key())) {    
118     if (key_press == KEY_F4) {
119       Print("\nToggling Speaker Port\n");
120       SerialPrintLevel(100,"\nToggling Speaker Port\n");
121       *doIBuzz = (*doIBuzz + 1) % 2;
122     } else if (key_press == KEY_F5) {
123       Print("\nMachine Restart\n");
124       SerialPrintLevel(100,"\nMachine Restart\n");
125       machine_real_restart();
126     }
127   }
128   return;
129 }
130
131
132
133 extern char BSS_START, BSS_END;
134
135 extern char end;
136
137
138 /* This is an ugly hack to get at the VM  memory */
139 ulong_t vm_range_start;
140 ulong_t vm_range_end;
141 ulong_t guest_kernel_start;
142 ulong_t guest_kernel_end;
143 /* ** */
144
145
146 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
147 {
148   uint_t address;
149
150   for (address=start;address<start+length;address+=PAGE_SIZE) { 
151     void *page;
152     pte_t pte = template_pte;
153     
154     page=Alloc_Page();
155     KASSERT(page);
156     
157     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
158
159     KASSERT(MapPage((void*)address,&pte,1));
160   }
161   
162   return 0;
163 }
164     
165
166
167 /*
168  * Kernel C code entry point.
169  * Initializes kernel subsystems, mounts filesystems,
170  * and spawns init process.
171  */
172 void Main(struct Boot_Info* bootInfo)
173 {
174   struct Kernel_Thread * key_thread;
175   struct Kernel_Thread * spkr_thread;
176
177   ulong_t doIBuzz = 0;
178
179   Init_BSS();
180   Init_Screen();
181
182   Init_Serial();
183   Init_Mem(bootInfo);
184   Init_CRC32();
185   Init_TSS();
186   Init_Interrupts();
187   Init_Scheduler();
188   Init_Traps();
189   Init_Timer();
190   Init_Keyboard();
191   Init_VM(bootInfo);
192   Init_Paging();
193
194   //  Init_IDE();
195
196   // Print("Done; stalling\n");
197
198
199   
200 #if 0
201   SerialPrint("Dumping VM kernel Code (first 128 bytes @ 0x%x)\n", 0x100000);
202   SerialMemDump((unsigned char *)0x100000, 256);
203   /*
204     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
205     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
206   */
207 #endif
208
209 #if 1
210   SerialPrint("Dumping BIOS code ffff0-fffff\n\n");
211   SerialMemDump((unsigned char *)0x10fff0, 16);
212   /*
213     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
214     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
215   */
216 #endif
217
218 #if 1
219   SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
220   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
221   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
222
223 #endif
224
225   {
226     RunVMM(bootInfo);
227   }
228
229
230   
231
232
233   TODO("Write a Virtual Machine Monitor");
234   
235
236   Exit(0);
237 }