| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <i386/i386.h> |
|---|
| 20 | #include <i386/pit.h> |
|---|
| 21 | #include <i386/smp.h> |
|---|
| 22 | #include <i386/cpuid.h> |
|---|
| 23 | #include <i386/virtual.h> |
|---|
| 24 | #include <i386/idt.h> |
|---|
| 25 | #include <i386/ioports.h> |
|---|
| 26 | #include <i386/pic.h> |
|---|
| 27 | #include <time.h> |
|---|
| 28 | #include <vmm.h> |
|---|
| 29 | |
|---|
| 30 | #define rdtsc(low,high) \ |
|---|
| 31 | asm volatile("rdtsc" : "=a"(low), "=d"(high)) |
|---|
| 32 | |
|---|
| 33 | #define CALIBRATE_LATCH (5*LATCH) |
|---|
| 34 | #define CALIBRATE_TIME (5*1000020/HZ) |
|---|
| 35 | |
|---|
| 36 | static DWORD CalibrateTsc() |
|---|
| 37 | { |
|---|
| 38 | DWORD startLow,startHigh,endLow,endHigh,count; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | outb(0x61,(inb(0x61) & ~0x02) | 0x01); |
|---|
| 42 | |
|---|
| 43 | outb(0x43,0xB0); |
|---|
| 44 | outb(0x42,CALIBRATE_LATCH & 0xFF); |
|---|
| 45 | outb(0x42,CALIBRATE_LATCH >> 8); |
|---|
| 46 | |
|---|
| 47 | rdtsc(startLow,startHigh); |
|---|
| 48 | count=0; |
|---|
| 49 | do |
|---|
| 50 | { |
|---|
| 51 | ++count; |
|---|
| 52 | }while (!(inb(0x61) & 0x20)); |
|---|
| 53 | |
|---|
| 54 | rdtsc(endLow,endHigh); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | asm("subl %2,%0 \n\t" |
|---|
| 58 | "sbbl %3,%1" : "=a"(endLow),"=d"(endHigh) |
|---|
| 59 | : "g"(startLow),"g"(startHigh),"g"(endLow),"1"(endHigh)); |
|---|
| 60 | |
|---|
| 61 | asm("divl %2" : "=a"(endLow),"=d"(endHigh) : "r"(endLow),"g"(0),"1"(CALIBRATE_TIME)); |
|---|
| 62 | return endLow; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | static void CalcCpuSpeed() |
|---|
| 67 | { |
|---|
| 68 | |
|---|
| 69 | if (CpuIdSupports(CPU_FEATURE_TSC)) |
|---|
| 70 | { |
|---|
| 71 | DWORD tscQ=CalibrateTsc(); |
|---|
| 72 | DWORD eax=0,edx=1000,cpuKhz; |
|---|
| 73 | asm("divl %2" : "=a"(cpuKhz),"=d"(edx):"r"(tscQ),"g"(eax),"1"(edx)); |
|---|
| 74 | |
|---|
| 75 | KePrint("CPU: Detected a %lu.%03lu MHz processor.\n",cpuKhz/1000,cpuKhz % 1000); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | int ArchInit() |
|---|
| 80 | { |
|---|
| 81 | KePrint("Whitix x86 v0.1.5. Built on %s at %s\n",__DATE__,__TIME__); |
|---|
| 82 | PhysInit(); |
|---|
| 83 | VirtEarlyInit(); |
|---|
| 84 | CpuIdInit(); |
|---|
| 85 | CalcCpuSpeed(); |
|---|
| 86 | |
|---|
| 87 | SmpInit(); |
|---|
| 88 | PicRemap(0x20,0x28); |
|---|
| 89 | IrqInit(); |
|---|
| 90 | IdtInit(); |
|---|
| 91 | IdtLoad(); |
|---|
| 92 | |
|---|
| 93 | PitInit(); |
|---|
| 94 | |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|