Sync to trunk head (r42241)
[reactos.git] / reactos / hal / halx86 / mp / apic.c
index 8f8310e..651500a 100644 (file)
 /* INCLUDE ***********************************************************************/
 
 #include <hal.h>
+#include <halfuncs.h> /* Not in PCH because only used for MP HAL */
+#include <rtlfuncs.h> /* Not in PCH because only used for MP HAL */
 #define NDEBUG
 #include <debug.h>
-#include <internal/ntoskrnl.h>
 
 /* GLOBALS ***********************************************************************/
 
@@ -55,7 +56,8 @@ ULONG lastregw[MAX_CPU];
 ULONG lastvalw[MAX_CPU];
 
 #ifdef CONFIG_SMP
-typedef struct __attribute__((packed)) _COMMON_AREA_INFO
+#include <pshpack1.h>
+typedef struct _COMMON_AREA_INFO
 {
    ULONG Stack;                    /* Location of AP stack */
    ULONG PageDirectory;            /* Page directory for an AP */
@@ -63,6 +65,7 @@ typedef struct __attribute__((packed)) _COMMON_AREA_INFO
    ULONG PaeModeEnabled;    /* PAE mode is enabled */
    ULONG Debug[16];        /* For debugging */
 } COMMON_AREA_INFO, *PCOMMON_AREA_INFO;
+#include <poppack.h>
 #endif
 
 CHAR *APstart, *APend;
@@ -73,15 +76,15 @@ CHAR *APstart, *APend;
 #define HZ             (100)
 #define APIC_DIVISOR   (16)
 
-#define CMOS_READ(address) ({ \
+#define CMOS_READ(address) { \
    WRITE_PORT_UCHAR((PUCHAR)0x70, address)); \
    READ_PORT_UCHAR((PUCHAR)0x71)); \
-})
+}
 
-#define CMOS_WRITE(address, value) ({ \
+#define CMOS_WRITE(address, value) { \
    WRITE_PORT_UCHAR((PUCHAR)0x70, address); \
    WRITE_PORT_UCHAR((PUCHAR)0x71, value); \
-})
+}
 
 extern ULONG_PTR KernelBase;
 
@@ -181,7 +184,7 @@ VOID EnableApicMode(VOID)
 }
 
 /* Disable symetric I/O mode ie. go to PIC mode */
-inline VOID DisableSMPMode(VOID)
+__inline VOID DisableSMPMode(VOID)
 {
    /*
     * Put the board back into PIC mode (has an effect
@@ -220,72 +223,6 @@ VOID APICDisable(VOID)
   APICWrite(APIC_SIVR, tmp);
 }
 
-
-inline ULONG _APICRead(ULONG Offset)
-{
-   PULONG p;
-
-   p = (PULONG)((ULONG)APICBase + Offset);
-   return *p;
-}
-
-#if 0
-inline VOID APICWrite(ULONG Offset,
-                     ULONG Value)
-{
-   PULONG p;
-
-   p = (PULONG)((ULONG)APICBase + Offset);
-
-   *p = Value;
-}
-#else
-inline VOID APICWrite(ULONG Offset,
-                     ULONG Value)
-{
-   PULONG p;
-   ULONG CPU = (_APICRead(APIC_ID) & APIC_ID_MASK) >> 24;
-
-   lastregw[CPU] = Offset;
-   lastvalw[CPU] = Value;
-
-   p = (PULONG)((ULONG)APICBase + Offset);
-
-   *p = Value;
-}
-#endif
-
-
-#if 0
-inline ULONG APICRead(ULONG Offset)
-{
-   PULONG p;
-
-   p = (PULONG)((ULONG)APICBase + Offset);
-   return *p;
-}
-#else
-inline ULONG APICRead(ULONG Offset)
-{
-   PULONG p;
-   ULONG CPU = (_APICRead(APIC_ID) & APIC_ID_MASK) >> 24;
-
-   lastregr[CPU] = Offset;
-   lastvalr[CPU] = 0;
-
-   p = (PULONG)((ULONG)APICBase + Offset);
-
-   lastvalr[CPU] = *p;
-   return lastvalr[CPU];
-}
-#endif
-
-inline VOID APICSendEOI(VOID)
-{
-  // Send the EOI
-  APICWrite(APIC_EOI, 0);
-}
-
 static VOID APICDumpBit(ULONG base)
 {
        ULONG v, i, j;
@@ -313,8 +250,7 @@ VOID APICDump(VOID)
 {
   ULONG v, ver, maxlvt;
   ULONG r1, r2, w1, w2;
-  ULONG CPU = ThisCPU();;
-
+  ULONG CPU = ThisCPU();
 
 
   r1 = lastregr[CPU];
@@ -414,7 +350,8 @@ VOID APICDump(VOID)
 
 BOOLEAN VerifyLocalAPIC(VOID)
 {
-   UINT reg0, reg1;
+   SIZE_T reg0, reg1;
+   ULONG l = 0, h = 0;
    /* The version register is read-only in a real APIC */
    reg0 = APICRead(APIC_VER);
    DPRINT1("Getting VERSION: %x\n", reg0);
@@ -461,15 +398,14 @@ BOOLEAN VerifyLocalAPIC(VOID)
       return FALSE;
    }
 
-   ULONG l, h;
-   Ki386Rdmsr(0x1b /*MSR_IA32_APICBASE*/, l, h);
+   Ke386Rdmsr(0x1b /*MSR_IA32_APICBASE*/, l, h);
 
    if (!(l & /*MSR_IA32_APICBASE_ENABLE*/(1<<11))) 
    {
       DPRINT1("Local APIC disabled by BIOS -- reenabling.\n");
       l &= ~/*MSR_IA32_APICBASE_BASE*/(1<<11);
       l |= /*MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE*/(1<<11)|0xfee00000;
-      Ki386Wrmsr(0x1b/*MSR_IA32_APICBASE*/, l, h);
+      Ke386Wrmsr(0x1b/*MSR_IA32_APICBASE*/, l, h);
    }
 
     
@@ -480,11 +416,11 @@ BOOLEAN VerifyLocalAPIC(VOID)
 #ifdef CONFIG_SMP
 VOID APICSendIPI(ULONG Target, ULONG Mode)
 {
-   ULONG tmp, i, flags;
+   ULONG tmp, i, flags = 0;
 
    /* save flags and disable interrupts */
-   Ki386SaveFlags(flags);
-   Ki386DisableInterrupts();
+   Ke386SaveFlags(flags);
+   _disable();
 
    /* Wait up to 100ms for the APIC to become ready */
    for (i = 0; i < 10000; i++) 
@@ -539,7 +475,7 @@ VOID APICSendIPI(ULONG Target, ULONG Mode)
    {
       DPRINT1("CPU(%d) Current IPI was not delivered after 100ms.\n", ThisCPU());
    }
-   Ki386RestoreFlags(flags);
+   Ke386RestoreFlags(flags);
 }
 #endif
 
@@ -748,10 +684,10 @@ VOID MpsIpiHandler(VOID)
 {
    KIRQL oldIrql;
 
-   HalBeginSystemInterrupt(IPI_VECTOR, 
-                           IPI_LEVEL
+   HalBeginSystemInterrupt(IPI_LEVEL,
+                           IPI_VECTOR
                           &oldIrql);
-   Ki386EnableInterrupts();
+   _enable();
 #if 0
    DbgPrint("(%s:%d) MpsIpiHandler on CPU%d, current irql is %d\n", 
             __FILE__,__LINE__, KeGetCurrentProcessorNumber(), KeGetCurrentIrql());
@@ -763,7 +699,7 @@ VOID MpsIpiHandler(VOID)
    DbgPrint("(%s:%d) MpsIpiHandler on CPU%d done\n", __FILE__,__LINE__, KeGetCurrentProcessorNumber());
 #endif
 
-   Ki386DisableInterrupts();
+   _disable();
    HalEndSystemInterrupt(oldIrql, 0);
 }
 #endif
@@ -772,6 +708,9 @@ VOID
 MpsIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
                           PKTRAP_FRAME TrapFrame)
 {
+#ifdef _M_AMD64
+    UNIMPLEMENTED;
+#else
    TrapFrame->SegGs     = (USHORT)IrqTrapFrame->Gs;
    TrapFrame->SegFs     = (USHORT)IrqTrapFrame->Fs;
    TrapFrame->SegEs     = (USHORT)IrqTrapFrame->Es;
@@ -787,6 +726,7 @@ MpsIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
    TrapFrame->Eip    = IrqTrapFrame->Eip;
    TrapFrame->SegCs     = IrqTrapFrame->Cs;
    TrapFrame->EFlags = IrqTrapFrame->Eflags;
+#endif
 }
 
 VOID
@@ -801,28 +741,29 @@ MpsTimerHandler(ULONG Vector, PKIRQ_TRAPFRAME Trapframe)
    HalBeginSystemInterrupt(LOCAL_TIMER_VECTOR, 
                            PROFILE_LEVEL, 
                           &oldIrql);
-   Ki386EnableInterrupts();
+   _enable();
 
 #if 0
    CPU = ThisCPU();
    if ((Count[CPU] % 100) == 0)
    {
-     DbgPrint("(%s:%d) MpsTimerHandler on CPU%d, irql = %d, epi = %x, KPCR = %x\n", __FILE__, __LINE__, CPU, oldIrql,Trapframe->Eip, KeGetCurrentKPCR());
+     DbgPrint("(%s:%d) MpsTimerHandler on CPU%d, irql = %d, epi = %x, KPCR = %x\n", __FILE__, __LINE__, CPU, oldIrql,Trapframe->Eip, KeGetPcr());
    }
    Count[CPU]++;
 #endif
 
+   /* FIXME: SMP is totally broken */
    MpsIRQTrapFrameToTrapFrame(Trapframe, &KernelTrapFrame);
    if (KeGetCurrentProcessorNumber() == 0)
    {
-      KeUpdateSystemTime(&KernelTrapFrame, oldIrql);
+      //KeUpdateSystemTime(&KernelTrapFrame, oldIrql);
    }
    else
    {
-      KeUpdateRunTime(&KernelTrapFrame, oldIrql);
+      //KeUpdateRunTime(&KernelTrapFrame, oldIrql);
    }
 
-   Ki386DisableInterrupts();
+   _disable();
    HalEndSystemInterrupt (oldIrql, 0);
 }
 
@@ -833,12 +774,12 @@ VOID APICSetupLVTT(ULONG ClockTicks)
    tmp = GET_APIC_VERSION(APICRead(APIC_VER));
    if (!APIC_INTEGRATED(tmp))
    {
-      tmp = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) | APIC_LVT_PERIODIC | LOCAL_TIMER_VECTOR;;
+      tmp = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) | APIC_LVT_PERIODIC | LOCAL_TIMER_VECTOR;
    }
    else
    {
       /* Periodic timer */
-      tmp = APIC_LVT_PERIODIC | LOCAL_TIMER_VECTOR;;
+      tmp = APIC_LVT_PERIODIC | LOCAL_TIMER_VECTOR;
    }
    APICWrite(APIC_LVTT, tmp);
 
@@ -860,36 +801,36 @@ APICCalibrateTimer(ULONG CPU)
 
    APICSetupLVTT(1000000000);
 
-   TSCPresent = ((PKIPCR)KeGetCurrentKPCR())->PrcbData.FeatureBits & X86_FEATURE_TSC ? TRUE : FALSE;
+   TSCPresent = KeGetCurrentPrcb()->FeatureBits & KF_RDTSC ? TRUE : FALSE;
 
    /*
     * The timer chip counts down to zero. Let's wait
     * for a wraparound to start exact measurement:
     * (the current tick might have been already half done)
     */
-   WaitFor8254Wraparound();
+   //WaitFor8254Wraparound();
 
    /*
     * We wrapped around just now. Let's start
     */
    if (TSCPresent)
    {
-      Ki386RdTSC(t1);
+      t1.QuadPart = (LONGLONG)__rdtsc();
    }
    tt1 = APICRead(APIC_CCRT);
 
-   WaitFor8254Wraparound();
+   //WaitFor8254Wraparound();
 
 
    tt2 = APICRead(APIC_CCRT);
    if (TSCPresent)
    {
-      Ki386RdTSC(t2);
+      t2.QuadPart = (LONGLONG)__rdtsc();
       CPUMap[CPU].CoreSpeed = (HZ * (t2.QuadPart - t1.QuadPart));
       DPRINT("CPU clock speed is %ld.%04ld MHz.\n",
             CPUMap[CPU].CoreSpeed/1000000,
             CPUMap[CPU].CoreSpeed%1000000);
-      ((PKIPCR)KeGetCurrentKPCR())->PrcbData.MHz = CPUMap[CPU].CoreSpeed/1000000;
+      KeGetCurrentPrcb()->MHz = CPUMap[CPU].CoreSpeed/1000000;
    }
 
    CPUMap[CPU].BusSpeed = (HZ * (long)(tt1 - tt2) * APIC_DIVISOR);
@@ -905,8 +846,25 @@ APICCalibrateTimer(ULONG CPU)
 }
 
 VOID 
-SetInterruptGate(ULONG index, ULONG address)
+SetInterruptGate(ULONG index, ULONG_PTR address)
 {
+#ifdef _M_AMD64
+  KIDTENTRY64 *idt;
+
+  idt = &KeGetPcr()->IdtBase[index];
+
+  idt->OffsetLow = address & 0xffff;
+  idt->Selector = KGDT_64_R0_CODE;
+  idt->IstIndex = 0;
+  idt->Reserved0 = 0;
+  idt->Type = 0x0e;
+  idt->Dpl = 0;
+  idt->Present = 1;
+  idt->OffsetMiddle = (address >> 16) & 0xffff;
+  idt->OffsetHigh = address >> 32;
+  idt->Reserved1 = 0;
+  idt->Alignment = 0;
+#else
   KIDTENTRY *idt;
   KIDT_ACCESS Access;
 
@@ -917,11 +875,12 @@ SetInterruptGate(ULONG index, ULONG address)
   Access.SystemSegmentFlag = 0;
   Access.SegmentType = I386_INTERRUPT_GATE;
   
-  idt = (KIDTENTRY*)((ULONG)KeGetCurrentKPCR()->IDT + index * sizeof(KIDTENTRY));
+  idt = (KIDTENTRY*)((ULONG)KeGetPcr()->IDT + index * sizeof(KIDTENTRY));
   idt->Offset = address & 0xffff;
   idt->Selector = KGDT_R0_CODE;
   idt->Access = Access.Value;
   idt->ExtendedOffset = address >> 16;
+#endif
 }
 
 VOID HaliInitBSP(VOID)
@@ -935,20 +894,20 @@ VOID HaliInitBSP(VOID)
    /* Only initialize the BSP once */
    if (BSPInitialized)
    {
-      KEBUGCHECK(0);
+      ASSERT(FALSE);
       return;
    }
 
    BSPInitialized = TRUE;
 
    /* Setup interrupt handlers */
-   SetInterruptGate(LOCAL_TIMER_VECTOR, (ULONG)MpsTimerInterrupt);
-   SetInterruptGate(ERROR_VECTOR, (ULONG)MpsErrorInterrupt);
-   SetInterruptGate(SPURIOUS_VECTOR, (ULONG)MpsSpuriousInterrupt);
+   SetInterruptGate(LOCAL_TIMER_VECTOR, (ULONG_PTR)MpsTimerInterrupt);
+   SetInterruptGate(ERROR_VECTOR, (ULONG_PTR)MpsErrorInterrupt);
+   SetInterruptGate(SPURIOUS_VECTOR, (ULONG_PTR)MpsSpuriousInterrupt);
 #ifdef CONFIG_SMP
-   SetInterruptGate(IPI_VECTOR, (ULONG)MpsIpiInterrupt);
+   SetInterruptGate(IPI_VECTOR, (ULONG_PTR)MpsIpiInterrupt);
 #endif
-   DPRINT("APIC is mapped at 0x%X\n", APICBase);
+   DPRINT1("APIC is mapped at 0x%p\n", (PVOID)APICBase);
 
    if (VerifyLocalAPIC()) 
    {
@@ -956,8 +915,8 @@ VOID HaliInitBSP(VOID)
    } 
    else 
    {
-      DPRINT("No APIC found\n");
-      KEBUGCHECK(0);
+      DPRINT1("No APIC found\n");
+      ASSERT(FALSE);
    }
 
    if (APICMode == amPIC) 
@@ -975,18 +934,18 @@ VOID HaliInitBSP(VOID)
    CommonBase = (PULONG)COMMON_AREA;
  
    /* Copy bootstrap code to common area */
-   memcpy((PVOID)((ULONG)CommonBase + PAGE_SIZE),
+   memcpy((PVOID)((ULONG_PTR)CommonBase + PAGE_SIZE),
          &APstart,
-         (ULONG)&APend - (ULONG)&APstart + 1);
+         (ULONG_PTR)&APend - (ULONG_PTR)&APstart + 1);
 
    /* Set shutdown code */
    CMOS_WRITE(0xF, 0xA);
 
    /* Set warm reset vector */
-   ps = (PUSHORT)((ULONG)BIOSBase + 0x467);
+   ps = (PUSHORT)((ULONG_PTR)BIOSBase + 0x467);
    *ps = (COMMON_AREA + PAGE_SIZE) & 0xF;
  
-   ps = (PUSHORT)((ULONG)BIOSBase + 0x469);
+   ps = (PUSHORT)((ULONG_PTR)BIOSBase + 0x469);
    *ps = (COMMON_AREA + PAGE_SIZE) >> 4;
 #endif
 
@@ -1009,7 +968,7 @@ HaliStartApplicationProcessor(ULONG Cpu, ULONG Stack)
        Cpu >= CPUCount ||
        OnlineCPUs & (1 << Cpu))
    {
-     KEBUGCHECK(0);
+     ASSERT(FALSE);
    }
    DPRINT1("Attempting to boot CPU %d\n", Cpu);
 
@@ -1035,11 +994,11 @@ HaliStartApplicationProcessor(ULONG Cpu, ULONG Stack)
    /* Write the location of the AP stack */
    Common->Stack = (ULONG)Stack;
    /* Write the page directory page */
-   Ke386GetPageTableDirectory(Common->PageDirectory);
+   Common->PageDirectory = __readcr3();
    /* Write the kernel entry point */
    Common->NtProcessStartup = (ULONG_PTR)RtlImageNtHeader((PVOID)KernelBase)->OptionalHeader.AddressOfEntryPoint + KernelBase;
    /* Write the state of the mae mode */
-   Common->PaeModeEnabled = Ke386GetCr4() & X86_CR4_PAE ? 1 : 0;
+   Common->PaeModeEnabled = __readcr4() & CR4_PAE ? 1 : 0;
 
    DPRINT1("%x %x %x %x\n", Common->Stack, Common->PageDirectory, Common->NtProcessStartup, Common->PaeModeEnabled);