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/debug.h>
13 #include <geekos/bootinfo.h>
14 #include <geekos/string.h>
15 #include <geekos/screen.h>
16 #include <geekos/mem.h>
17 #include <geekos/crc32.h>
18 #include <geekos/tss.h>
19 #include <geekos/int.h>
20 #include <geekos/kthread.h>
21 #include <geekos/trap.h>
22 #include <geekos/timer.h>
23 #include <geekos/keyboard.h>
24 #include <geekos/io.h>
25 #include <geekos/serial.h>
26 #include <geekos/reboot.h>
27 #include <geekos/mem.h>
28 #include <geekos/paging.h>
29 #include <geekos/ide.h>
30 #include <geekos/vm_cons.h>
31 #include <geekos/pci.h>
32 #include <geekos/gdt.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();
63 extern void Invalidate_PG(void * addr);
68 #define SPEAKER_PORT 0x61
71 void Buzz(unsigned delay, unsigned num)
77 init=In_Byte(SPEAKER_PORT);
80 Out_Byte(SPEAKER_PORT, init|0x2);
81 for (j=0;j<delay;j++) {
84 Out_Byte(SPEAKER_PORT, init);
85 for (j=0;j<delay;j++) {
91 inline void MyOut_Byte(ushort_t port, uchar_t value)
93 __asm__ __volatile__ (
96 : "a" (value), "Nd" (port)
101 * Read a byte from an I/O port.
103 inline uchar_t MyIn_Byte(ushort_t port)
107 __asm__ __volatile__ (
117 extern void MyBuzzVM();
119 #define MYBUZZVM_START MyBuzzVM
120 #define MYBUZZVM_LEN 0x3d
129 init=MyIn_Byte(SPEAKER_PORT);
132 MyOut_Byte(SPEAKER_PORT, init|0x2);
133 for (j=0;j<1000000;j++) {
136 MyOut_Byte(SPEAKER_PORT, init);
137 for (j=0;j<1000000;j++) {
159 void Buzzer(ulong_t arg) {
160 ulong_t *doIBuzz = (ulong_t*)arg;
162 // Quick and dirty hack to save my hearing...
163 // I'm not too worried about timing, so I'll deal with concurrency later...
173 void Hello(ulong_t arg)
182 __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
187 void Keyboard_Listener(ulong_t arg) {
188 ulong_t * doIBuzz = (ulong_t*)arg;
191 Print("Press F4 to turn on/off the speaker\n");
193 while ((key_press = Wait_For_Key())) {
194 if (key_press == KEY_F4) {
195 PrintBoth("\nToggling Speaker Port\n");
196 *doIBuzz = (*doIBuzz + 1) % 2;
197 } else if (key_press == KEY_F5) {
198 PrintBoth("\nMachine Restart\n");
199 machine_real_restart();
207 extern char BSS_START, BSS_END;
214 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
218 for (address=start;address<start+length;address+=PAGE_SIZE) {
220 pte_t pte = template_pte;
225 pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
227 KASSERT(MapPage((void*)address,&pte,1));
236 * Kernel C code entry point.
237 * Initializes kernel subsystems, mounts filesystems,
238 * and spawns init process.
240 void Main(struct Boot_Info* bootInfo)
242 struct Kernel_Thread * key_thread;
243 struct Kernel_Thread * spkr_thread;
274 PrintBoth("\n\nHello, Welcome to this horrid output-only serial interface\n");
275 PrintBoth("Eventually, this will let us control the VMM\n\n");
277 PrintBoth("\n\n===>");
284 PrintBoth("Launching Noisemaker and keyboard listener threads\n");
286 key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
287 spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
293 PrintBoth("Next: setup GDT\n");
295 uint_t addr = 0xe0000;
300 // wrmsr(SYMBIOTIC_MSR, hi, addr);
302 uint_t msr_num = 0x0000001B;
303 __asm__ __volatile__ ("rdmsr" : : "c"(msr_num) : "%eax","%edx","memory");
306 uint_t msr_num = 0x0000001c;
307 __asm__ __volatile__ ("rdmsr" : : "c"(msr_num) : "%eax","%edx","memory");
312 uint_t msr_num = 0x0000001B;
313 __asm__ __volatile__ ("wrmsr" : : "c"(msr_num), "a"(hi), "d"(addr) : "memory");
319 uint_t msr_num = 0x535;
320 __asm__ __volatile__ ("wrmsr" : : "c"(msr_num), "a"(hi), "d"(addr) : "memory");
329 for (i = 0; i < 1024; i++) {
330 uint_t * addr = (uint_t *)0xa00000;
333 PrintBoth("Read From 0x%x=%d\n", (uint_t)addr, foo);
337 // Invalidate_PG((void *)0x2000);
339 // VM_Test(bootInfo, 32);
340 //VM_Test(bootInfo, 1536);
352 /* Now this thread is done. */