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.


added queue implementation
[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.43 $
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 #include <geekos/vmm_stubs.h>
38
39 #include <geekos/pci.h>
40
41 #include <geekos/ne2k.h>
42 #include <uip/uip.h>
43 #include <uip/uip_arp.h>
44
45 #define SPEAKER_PORT 0x61
46 #define TEST_NE2K 0
47 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
48
49 #if TEST_NE2K
50 u8_t uip_buf[UIP_BUFSIZE+2];
51 u16_t uip_len;
52
53 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) {
54   uip_len = info->size;  
55   int i;
56   for(i = 0; i < info->size; i++) {
57     PrintBoth("%x ", *(pkt+i));
58     uip_buf[i] = *(pkt+i);
59     if(i % 10 == 0)
60       PrintBoth("\n");
61   }
62   Free(pkt);
63   if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
64     uip_arp_arpin();
65   } else {
66     uip_arp_ipin();
67     uip_input();
68   }
69   return 0;
70 }
71 #endif
72
73 void Spin()
74 {
75   // hack - competing thread
76   while (1) {};
77
78 }
79
80
81 void Buzz(unsigned delay, unsigned num)
82 {
83   volatile int x;
84   int i,j;
85   unsigned char init;
86   
87   init=In_Byte(SPEAKER_PORT);
88
89   for (i=0;i<num;i++) { 
90     Out_Byte(SPEAKER_PORT, init|0x2);
91     for (j=0;j<delay;j++) { 
92       x+=j;
93     }
94     Out_Byte(SPEAKER_PORT, init);
95     for (j=0;j<delay;j++) { 
96       x+=j;
97     }
98   }
99 }
100
101 inline void MyOut_Byte(ushort_t port, uchar_t value)
102 {
103     __asm__ __volatile__ (
104         "outb %b0, %w1"
105         :
106         : "a" (value), "Nd" (port)
107     );
108 }
109
110 /*
111  * Read a byte from an I/O port.
112  */
113 inline uchar_t MyIn_Byte(ushort_t port)
114 {
115     uchar_t value;
116
117     __asm__ __volatile__ (
118         "inb %w1, %b0"
119         : "=a" (value)
120         : "Nd" (port)
121     );
122
123     return value;
124 }
125
126
127
128
129
130
131
132 void Buzzer(ulong_t arg) {
133   ulong_t *doIBuzz = (ulong_t*)arg;
134   while (1) {
135     // Quick and dirty hack to save my hearing...
136     // I'm not too worried about timing, so I'll deal with concurrency later...
137     if (*doIBuzz == 1) {
138       Buzz(1000000, 10);
139     }
140   }
141
142 }
143
144
145
146
147
148 void Keyboard_Listener(ulong_t arg) {
149   ulong_t * doIBuzz = (ulong_t*)arg;
150   Keycode key_press;
151
152   Print("Press F4 to turn on/off the speaker\n");
153
154   while ((key_press = Wait_For_Key())) {    
155     if (key_press == KEY_F4) {
156       Print("\nToggling Speaker Port\n");
157       SerialPrintLevel(100,"\nToggling Speaker Port\n");
158       *doIBuzz = (*doIBuzz + 1) % 2;
159     } else if (key_press == KEY_F5) {
160       Print("\nMachine Restart\n");
161       SerialPrintLevel(100,"\nMachine Restart\n");
162       machine_real_restart();
163     }
164   }
165   return;
166 }
167
168
169
170 extern char BSS_START, BSS_END;
171
172 extern char end;
173
174
175 /* This is an ugly hack to get at the VM  memory */
176 ulong_t vm_range_start;
177 ulong_t vm_range_end;
178 ulong_t guest_kernel_start;
179 ulong_t guest_kernel_end;
180 /* ** */
181
182
183 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
184 {
185   uint_t address;
186
187   for (address=start;address<start+length;address+=PAGE_SIZE) { 
188     void *page;
189     pte_t pte = template_pte;
190     
191     page=Alloc_Page();
192     KASSERT(page);
193     
194     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
195
196     KASSERT(MapPage((void*)address,&pte,1));
197   }
198   
199   return 0;
200 }
201     
202
203
204 /*
205  * Kernel C code entry point.
206  * Initializes kernel subsystems, mounts filesystems,
207  * and spawns init process.
208  */
209 void Main(struct Boot_Info* bootInfo)
210 {
211
212
213   //Out_Byte(0x1234,5);
214   //Out_Byte(0x1234,5);
215
216   Init_BSS();
217   Init_Screen();
218
219   Init_Serial();
220
221   /*  {
222     extern char BSS_START, BSS_END;
223
224     SerialPrint("BSS 0x%x->0x%x\n", &BSS_START, &BSS_END);
225
226     }*/
227
228
229   // SerialPrint("Guest Mem Dump at 0x%x\n", 0x100000);
230   //SerialMemDump((unsigned char *)(0x100000), 261 * 1024);
231
232   Init_Mem(bootInfo);
233   Init_CRC32();
234   Init_TSS();
235   Init_Interrupts();
236   Init_Scheduler();
237   Init_Traps();
238   Init_Timer();
239   Init_Keyboard();
240   Init_VM(bootInfo);
241   Init_Paging();
242   
243   //Init_PCI();
244
245   Init_Stubs();
246
247 #if TEST_NE2K
248   Init_Ne2k(&Packet_Received);
249   uip_init();
250   uip_arp_init();
251
252   uip_ipaddr_t ipaddr;
253   uip_ipaddr(ipaddr, 10,0,2,21);  /* Local IP address */
254   uip_sethostaddr(ipaddr);
255
256   uip_ipaddr_t ripaddr;
257   uip_ipaddr(ripaddr, 10,0,2,20);  /* Remote IP address */
258   
259   /* Attempt a connection to port 8080 at address 10.0.2.20 */
260   struct uip_conn *conn;
261   conn = uip_connect(&ripaddr, HTONS(8080));
262
263   while(uip_len <= 0) {
264     uip_periodic_conn(conn);
265   }
266   NE2K_Transmit(uip_len+UIP_LLH_LEN);  /* This will transmit an ARP packet */
267
268   int l = 0;
269   while(l++ < 5000); /* When this is done, a response to the ARP should have been received. */
270   conn = uip_connect(&ripaddr, HTONS(8080));
271
272   while(uip_len <= 0) {
273     uip_periodic_conn(conn);
274   }
275   NE2K_Transmit(uip_len+UIP_LLH_LEN); /* This *should* transmit a SYN packet */
276
277 #endif /* TEST_NE2K */
278
279   //  Init_IDE();
280
281   // Print("Done; stalling\n");
282
283
284   
285 #if 0
286   SerialPrint("Dumping VM kernel Code (first 128 bytes @ 0x%x)\n", 0x100000);
287   SerialMemDump((unsigned char *)0x100000, 256);
288   /*
289     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
290     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
291   */
292 #endif
293
294
295 #if 1
296   struct Kernel_Thread *spin_thread;
297
298   spin_thread=Start_Kernel_Thread(Spin,0,PRIORITY_NORMAL,false);
299
300 #endif
301
302 #if 0
303   {
304
305   struct Kernel_Thread * key_thread;
306   struct Kernel_Thread * spkr_thread;
307
308   ulong_t doIBuzz = 0;
309
310   SerialPrint("Dumping BIOS code ffff0-fffff\n\n");
311   SerialMemDump((unsigned char *)0x10fff0, 16);
312   /*
313     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
314     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
315   */
316
317   SerialPrint("Noisemaker and keyboard listener threads\n");
318   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
319   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
320   }
321 #endif
322
323 #if !TEST_NE2K
324   {
325     RunVMM(bootInfo);
326   }
327 #endif
328
329
330   SerialPrint("RunVMM returned, spinning\n");
331   while (1) {} 
332
333
334   TODO("Write a Virtual Machine Monitor");
335   
336
337   Exit(0);
338 }