2004-11-28 01:30:02 +00:00
|
|
|
/*
|
|
|
|
* ReactOS kernel
|
2005-03-07 16:40:32 +00:00
|
|
|
* Copyright (C) 2004, 2005 ReactOS Team
|
2004-11-28 01:30:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2009-10-27 10:34:16 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-11-28 01:30:02 +00:00
|
|
|
*/
|
2013-03-14 21:04:13 +00:00
|
|
|
/*
|
2004-11-28 01:30:02 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
2010-01-21 13:43:49 +00:00
|
|
|
* FILE: hal/halx86/mp/apic.c
|
2021-06-11 12:29:21 +00:00
|
|
|
* PURPOSE:
|
|
|
|
* PROGRAMMER:
|
2004-11-28 01:30:02 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
/* INCLUDE ***********************************************************************/
|
|
|
|
|
2005-06-18 14:28:28 +00:00
|
|
|
#include <hal.h>
|
2006-05-10 17:47:44 +00:00
|
|
|
#include <halfuncs.h> /* Not in PCH because only used for MP HAL */
|
|
|
|
#include <rtlfuncs.h> /* Not in PCH because only used for MP HAL */
|
2005-06-19 22:53:49 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
/* GLOBALS ***********************************************************************/
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
ULONG CPUCount; /* Total number of CPUs */
|
|
|
|
ULONG BootCPU; /* Bootstrap processor */
|
|
|
|
ULONG OnlineCPUs; /* Bitmask of online CPUs */
|
|
|
|
CPU_INFO CPUMap[MAX_CPU]; /* Map of all CPUs in the system */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
PULONG BIOSBase; /* Virtual address of BIOS data segment */
|
|
|
|
PULONG CommonBase; /* Virtual address of common area */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PULONG APICBase = (PULONG)APIC_DEFAULT_BASE; /* Virtual address of local APIC */
|
|
|
|
|
|
|
|
ULONG APICMode; /* APIC mode at startup */
|
|
|
|
|
|
|
|
/* For debugging */
|
|
|
|
ULONG lastregr[MAX_CPU];
|
|
|
|
ULONG lastvalr[MAX_CPU];
|
|
|
|
ULONG lastregw[MAX_CPU];
|
|
|
|
ULONG lastvalw[MAX_CPU];
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2006-01-08 06:30:00 +00:00
|
|
|
#include <pshpack1.h>
|
|
|
|
typedef struct _COMMON_AREA_INFO
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
ULONG Stack; /* Location of AP stack */
|
|
|
|
ULONG PageDirectory; /* Page directory for an AP */
|
|
|
|
ULONG NtProcessStartup; /* Kernel entry point for an AP */
|
|
|
|
ULONG PaeModeEnabled; /* PAE mode is enabled */
|
|
|
|
ULONG Debug[16]; /* For debugging */
|
|
|
|
} COMMON_AREA_INFO, *PCOMMON_AREA_INFO;
|
2006-01-08 06:30:00 +00:00
|
|
|
#include <poppack.h>
|
2005-03-07 16:40:32 +00:00
|
|
|
#endif
|
|
|
|
|
2009-10-18 18:52:56 +00:00
|
|
|
extern CHAR *APstart, *APend;
|
2005-03-07 16:40:32 +00:00
|
|
|
|
|
|
|
#define BIOS_AREA 0x0
|
|
|
|
#define COMMON_AREA 0x2000
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
#define HZ (100)
|
|
|
|
#define APIC_DIVISOR (16)
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2006-01-08 06:30:00 +00:00
|
|
|
#define CMOS_READ(address) { \
|
2004-11-28 01:30:02 +00:00
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x70, address)); \
|
|
|
|
READ_PORT_UCHAR((PUCHAR)0x71)); \
|
2006-01-08 06:30:00 +00:00
|
|
|
}
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2006-01-08 06:30:00 +00:00
|
|
|
#define CMOS_WRITE(address, value) { \
|
2004-11-28 01:30:02 +00:00
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x70, address); \
|
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x71, value); \
|
2006-01-08 06:30:00 +00:00
|
|
|
}
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2005-07-16 13:01:18 +00:00
|
|
|
extern ULONG_PTR KernelBase;
|
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
/* FUNCTIONS *********************************************************************/
|
2004-11-28 01:30:02 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
extern ULONG Read8254Timer(VOID);
|
|
|
|
extern VOID WaitFor8254Wraparound(VOID);
|
|
|
|
extern VOID MpsTimerInterrupt(VOID);
|
|
|
|
extern VOID MpsErrorInterrupt(VOID);
|
|
|
|
extern VOID MpsSpuriousInterrupt(VOID);
|
|
|
|
extern VOID MpsIpiInterrupt(VOID);
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
ULONG APICGetMaxLVT(VOID)
|
|
|
|
{
|
|
|
|
ULONG tmp, ver, maxlvt;
|
|
|
|
|
|
|
|
tmp = APICRead(APIC_VER);
|
|
|
|
ver = GET_APIC_VERSION(tmp);
|
|
|
|
/* 82489DXs do not report # of LVT entries. */
|
|
|
|
maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(tmp) : 2;
|
|
|
|
|
|
|
|
return maxlvt;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID APICClear(VOID)
|
|
|
|
{
|
|
|
|
ULONG tmp, maxlvt;
|
|
|
|
|
|
|
|
maxlvt = APICGetMaxLVT();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Careful: we have to set masks only first to deassert
|
|
|
|
* any level-triggered sources.
|
|
|
|
*/
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt >= 3)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = ERROR_VECTOR;
|
|
|
|
APICWrite(APIC_LVT3, tmp | APIC_LVT3_MASKED);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = APICRead(APIC_LVTT);
|
|
|
|
APICWrite(APIC_LVTT, tmp | APIC_LVT_MASKED);
|
|
|
|
|
|
|
|
tmp = APICRead(APIC_LINT0);
|
|
|
|
APICWrite(APIC_LINT0, tmp | APIC_LVT_MASKED);
|
|
|
|
|
|
|
|
tmp = APICRead(APIC_LINT1);
|
|
|
|
APICWrite(APIC_LINT1, tmp | APIC_LVT_MASKED);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt >= 4)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = APICRead(APIC_LVTPC);
|
|
|
|
APICWrite(APIC_LVTPC, tmp | APIC_LVT_MASKED);
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
if (maxlvt >= 5)
|
|
|
|
{
|
|
|
|
tmp = APICRead(APIC_LVTTHMR);
|
|
|
|
APICWrite(APIC_LVTTHMR, tmp | APIC_LVT_MASKED);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Clean APIC state for other OSs:
|
|
|
|
*/
|
|
|
|
APICWrite(APIC_LVTT, APIC_LVT_MASKED);
|
|
|
|
APICWrite(APIC_LINT0, APIC_LVT_MASKED);
|
|
|
|
APICWrite(APIC_LINT1, APIC_LVT_MASKED);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt >= 3)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
APICWrite(APIC_LVT3, APIC_LVT3_MASKED);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt >= 4)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
APICWrite(APIC_LVTPC, APIC_LVT_MASKED);
|
|
|
|
}
|
|
|
|
#if 0
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt >= 5)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
APICWrite(APIC_LVTTHMR, APIC_LVT_MASKED);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable symetric I/O mode ie. connect the BSP's local APIC to INT and NMI lines */
|
2005-03-07 16:40:32 +00:00
|
|
|
VOID EnableApicMode(VOID)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Do not trust the local APIC being empty at bootup.
|
|
|
|
*/
|
|
|
|
APICClear();
|
|
|
|
|
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x22, 0x70);
|
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x23, 0x01);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable symetric I/O mode ie. go to PIC mode */
|
2005-11-28 23:35:35 +00:00
|
|
|
__inline VOID DisableSMPMode(VOID)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Put the board back into PIC mode (has an effect
|
|
|
|
* only on certain older boards). Note that APIC
|
|
|
|
* interrupts, including IPIs, won't work beyond
|
|
|
|
* this point! The only exception are INIT IPIs.
|
|
|
|
*/
|
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x22, 0x70);
|
|
|
|
WRITE_PORT_UCHAR((PUCHAR)0x23, 0x00);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID DumpESR(VOID)
|
|
|
|
{
|
|
|
|
ULONG tmp;
|
|
|
|
|
|
|
|
if (APICGetMaxLVT() > 3)
|
|
|
|
{
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
}
|
|
|
|
tmp = APICRead(APIC_ESR);
|
|
|
|
DbgPrint("ESR %08x\n", tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID APICDisable(VOID)
|
|
|
|
{
|
|
|
|
ULONG tmp;
|
|
|
|
|
|
|
|
APICClear();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable APIC (implies clearing of registers for 82489DX!).
|
|
|
|
*/
|
|
|
|
tmp = APICRead(APIC_SIVR);
|
|
|
|
tmp &= ~APIC_SIVR_ENABLE;
|
|
|
|
APICWrite(APIC_SIVR, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID APICDumpBit(ULONG base)
|
|
|
|
{
|
|
|
|
ULONG v, i, j;
|
|
|
|
|
|
|
|
DbgPrint("0123456789abcdef0123456789abcdef\n");
|
2021-06-11 12:29:21 +00:00
|
|
|
for (i = 0; i < 8; i++)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
v = APICRead(base + i*0x10);
|
2021-06-11 12:29:21 +00:00
|
|
|
for (j = 0; j < 32; j++)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
if (v & (1<<j))
|
|
|
|
DbgPrint("1");
|
|
|
|
else
|
|
|
|
DbgPrint("0");
|
|
|
|
}
|
|
|
|
DbgPrint("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID APICDump(VOID)
|
|
|
|
/*
|
|
|
|
* Dump the contents of the local APIC registers
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
ULONG v, ver, maxlvt;
|
|
|
|
ULONG r1, r2, w1, w2;
|
2008-12-25 12:27:47 +00:00
|
|
|
ULONG CPU = ThisCPU();
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
r1 = lastregr[CPU];
|
|
|
|
r2 = lastvalr[CPU];
|
|
|
|
w1 = lastregw[CPU];
|
|
|
|
w2 = lastvalw[CPU];
|
|
|
|
|
|
|
|
DbgPrint("\nPrinting local APIC contents on CPU(%d):\n", ThisCPU());
|
|
|
|
v = APICRead(APIC_ID);
|
|
|
|
DbgPrint("... ID : %08x (%01x) ", v, GET_APIC_ID(v));
|
|
|
|
v = APICRead(APIC_VER);
|
|
|
|
DbgPrint("... VERSION: %08x\n", v);
|
|
|
|
ver = GET_APIC_VERSION(v);
|
|
|
|
maxlvt = APICGetMaxLVT();
|
|
|
|
|
|
|
|
v = APICRead(APIC_TPR);
|
|
|
|
DbgPrint("... TPR : %08x (%02x)", v, v & ~0);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (APIC_INTEGRATED(ver))
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/* !82489DX */
|
|
|
|
v = APICRead(APIC_APR);
|
|
|
|
DbgPrint("... APR : %08x (%02x)\n", v, v & ~0);
|
|
|
|
v = APICRead(APIC_PPR);
|
|
|
|
DbgPrint("... PPR : %08x\n", v);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = APICRead(APIC_EOI);
|
|
|
|
DbgPrint("... EOI : %08x ! ", v);
|
|
|
|
v = APICRead(APIC_LDR);
|
|
|
|
DbgPrint("... LDR : %08x\n", v);
|
|
|
|
v = APICRead(APIC_DFR);
|
|
|
|
DbgPrint("... DFR : %08x ! ", v);
|
|
|
|
v = APICRead(APIC_SIVR);
|
|
|
|
DbgPrint("... SIVR : %08x\n", v);
|
|
|
|
|
|
|
|
if (0)
|
|
|
|
{
|
|
|
|
DbgPrint("... ISR field:\n");
|
|
|
|
APICDumpBit(APIC_ISR);
|
|
|
|
DbgPrint("... TMR field:\n");
|
|
|
|
APICDumpBit(APIC_TMR);
|
|
|
|
DbgPrint("... IRR field:\n");
|
|
|
|
APICDumpBit(APIC_IRR);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (APIC_INTEGRATED(ver))
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/* !82489DX */
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt > 3)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/* Due to the Pentium erratum 3AP. */
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
}
|
|
|
|
v = APICRead(APIC_ESR);
|
|
|
|
DbgPrint("... ESR : %08x\n", v);
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2004-11-28 01:30:02 +00:00
|
|
|
v = APICRead(APIC_ICR0);
|
|
|
|
DbgPrint("... ICR0 : %08x ! ", v);
|
|
|
|
v = APICRead(APIC_ICR1);
|
|
|
|
DbgPrint("... ICR1 : %08x ! ", v);
|
|
|
|
|
|
|
|
v = APICRead(APIC_LVTT);
|
|
|
|
DbgPrint("... LVTT : %08x\n", v);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt > 3)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/* PC is LVT#4. */
|
|
|
|
v = APICRead(APIC_LVTPC);
|
|
|
|
DbgPrint("... LVTPC : %08x ! ", v);
|
|
|
|
}
|
|
|
|
v = APICRead(APIC_LINT0);
|
|
|
|
DbgPrint("... LINT0 : %08x ! ", v);
|
|
|
|
v = APICRead(APIC_LINT1);
|
|
|
|
DbgPrint("... LINT1 : %08x\n", v);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt > 2)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
v = APICRead(APIC_LVT3);
|
|
|
|
DbgPrint("... LVT3 : %08x\n", v);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = APICRead(APIC_ICRT);
|
|
|
|
DbgPrint("... ICRT : %08x ! ", v);
|
|
|
|
v = APICRead(APIC_CCRT);
|
|
|
|
DbgPrint("... CCCT : %08x ! ", v);
|
|
|
|
v = APICRead(APIC_TDCR);
|
|
|
|
DbgPrint("... TDCR : %08x\n", v);
|
|
|
|
DbgPrint("\n");
|
|
|
|
DbgPrint("Last register read (offset): 0x%08X\n", r1);
|
|
|
|
DbgPrint("Last register read (value): 0x%08X\n", r2);
|
|
|
|
DbgPrint("Last register written (offset): 0x%08X\n", w1);
|
|
|
|
DbgPrint("Last register written (value): 0x%08X\n", w2);
|
|
|
|
DbgPrint("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN VerifyLocalAPIC(VOID)
|
|
|
|
{
|
2006-01-08 06:30:00 +00:00
|
|
|
SIZE_T reg0, reg1;
|
2009-09-18 20:56:49 +00:00
|
|
|
LARGE_INTEGER MsrValue;
|
|
|
|
|
2004-11-28 01:30:02 +00:00
|
|
|
/* The version register is read-only in a real APIC */
|
|
|
|
reg0 = APICRead(APIC_VER);
|
|
|
|
DPRINT1("Getting VERSION: %x\n", reg0);
|
|
|
|
APICWrite(APIC_VER, reg0 ^ APIC_VER_MASK);
|
|
|
|
reg1 = APICRead(APIC_VER);
|
|
|
|
DPRINT1("Getting VERSION: %x\n", reg1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The two version reads above should print the same
|
|
|
|
* numbers. If the second one is different, then we
|
|
|
|
* poke at a non-APIC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (reg1 != reg0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the version looks reasonably.
|
|
|
|
*/
|
|
|
|
reg1 = GET_APIC_VERSION(reg0);
|
|
|
|
if (reg1 == 0x00 || reg1 == 0xff)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
reg1 = APICGetMaxLVT();
|
|
|
|
if (reg1 < 0x02 || reg1 == 0xff)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The ID register is read/write in a real APIC.
|
|
|
|
*/
|
|
|
|
reg0 = APICRead(APIC_ID);
|
|
|
|
DPRINT1("Getting ID: %x\n", reg0);
|
|
|
|
APICWrite(APIC_ID, reg0 ^ APIC_ID_MASK);
|
|
|
|
reg1 = APICRead(APIC_ID);
|
|
|
|
DPRINT1("Getting ID: %x\n", reg1);
|
|
|
|
APICWrite(APIC_ID, reg0);
|
|
|
|
if (reg1 != (reg0 ^ APIC_ID_MASK))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-09-18 20:56:49 +00:00
|
|
|
MsrValue.QuadPart = __readmsr(0x1B /*MSR_IA32_APICBASE*/);
|
2005-03-07 16:40:32 +00:00
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (!(MsrValue.LowPart & /*MSR_IA32_APICBASE_ENABLE*/(1<<11)))
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DPRINT1("Local APIC disabled by BIOS -- reenabling.\n");
|
2009-09-18 20:56:49 +00:00
|
|
|
MsrValue.LowPart &= ~/*MSR_IA32_APICBASE_BASE*/(1<<11);
|
|
|
|
MsrValue.LowPart |= /*MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE*/(1<<11)|0xfee00000;
|
|
|
|
__writemsr(0x1B /*MSR_IA32_APICBASE*/, MsrValue.HighPart);
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
|
2004-11-28 01:30:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2004-11-28 01:30:02 +00:00
|
|
|
VOID APICSendIPI(ULONG Target, ULONG Mode)
|
|
|
|
{
|
2009-09-18 20:56:49 +00:00
|
|
|
ULONG tmp, i, flags;
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
/* save flags and disable interrupts */
|
2009-09-18 20:56:49 +00:00
|
|
|
flags = __readeflags();
|
2006-11-17 04:18:41 +00:00
|
|
|
_disable();
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
/* Wait up to 100ms for the APIC to become ready */
|
2021-06-11 12:29:21 +00:00
|
|
|
for (i = 0; i < 10000; i++)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = APICRead(APIC_ICR0);
|
|
|
|
/* Check Delivery Status */
|
|
|
|
if ((tmp & APIC_ICR0_DS) == 0)
|
|
|
|
break;
|
|
|
|
KeStallExecutionProcessor(10);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (i == 10000)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
DPRINT1("CPU(%d) Previous IPI was not delivered after 100ms.\n", ThisCPU());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the APIC to deliver the IPI */
|
|
|
|
DPRINT("%08x %x\n", SET_APIC_DEST_FIELD(Target), Target);
|
|
|
|
APICWrite(APIC_ICR1, SET_APIC_DEST_FIELD(Target));
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (Target == APIC_TARGET_SELF)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
Mode |= APIC_ICR0_DESTS_SELF;
|
2021-06-11 12:29:21 +00:00
|
|
|
}
|
|
|
|
else if (Target == APIC_TARGET_ALL)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
Mode |= APIC_ICR0_DESTS_ALL;
|
2021-06-11 12:29:21 +00:00
|
|
|
}
|
|
|
|
else if (Target == APIC_TARGET_ALL_BUT_SELF)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
Mode |= APIC_ICR0_DESTS_ALL_BUT_SELF;
|
2021-06-11 12:29:21 +00:00
|
|
|
}
|
|
|
|
else
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
Mode |= APIC_ICR0_DESTS_FIELD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, fire off the IPI */
|
|
|
|
APICWrite(APIC_ICR0, Mode);
|
|
|
|
|
|
|
|
/* Wait up to 100ms for the APIC to become ready */
|
2021-06-11 12:29:21 +00:00
|
|
|
for (i = 0; i < 10000; i++)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = APICRead(APIC_ICR0);
|
|
|
|
/* Check Delivery Status */
|
|
|
|
if ((tmp & APIC_ICR0_DS) == 0)
|
|
|
|
break;
|
|
|
|
KeStallExecutionProcessor(10);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (i == 10000)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
DPRINT1("CPU(%d) Current IPI was not delivered after 100ms.\n", ThisCPU());
|
|
|
|
}
|
2009-09-18 20:56:49 +00:00
|
|
|
__writeeflags(flags);
|
2004-11-28 01:30:02 +00:00
|
|
|
}
|
2005-03-07 16:40:32 +00:00
|
|
|
#endif
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
VOID APICSetup(VOID)
|
|
|
|
{
|
|
|
|
ULONG CPU, tmp;
|
|
|
|
|
|
|
|
CPU = ThisCPU();
|
|
|
|
|
|
|
|
// APICDump();
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2004-11-28 01:30:02 +00:00
|
|
|
DPRINT1("CPU%d:\n", CPU);
|
|
|
|
DPRINT1(" Physical APIC id: %d\n", GET_APIC_ID(APICRead(APIC_ID)));
|
|
|
|
DPRINT1(" Logical APIC id: %d\n", GET_APIC_LOGICAL_ID(APICRead(APIC_LDR)));
|
|
|
|
DPRINT1("%08x %08x %08x\n", APICRead(APIC_ID), APICRead(APIC_LDR), APICRead(APIC_DFR));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Intel recommends to set DFR, LDR and TPR before enabling
|
|
|
|
* an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
|
|
|
|
* document number 292116). So here it goes...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the APIC into flat delivery mode.
|
|
|
|
* Must be "all ones" explicitly for 82489DX.
|
|
|
|
*/
|
|
|
|
APICWrite(APIC_DFR, 0xFFFFFFFF);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the logical destination ID.
|
|
|
|
*/
|
|
|
|
tmp = APICRead(APIC_LDR);
|
|
|
|
tmp &= ~APIC_LDR_MASK;
|
2021-06-11 12:29:21 +00:00
|
|
|
/*
|
2004-11-28 01:30:02 +00:00
|
|
|
* FIXME:
|
|
|
|
* This works only up to 8 CPU's
|
|
|
|
*/
|
|
|
|
tmp |= (1 << (KeGetCurrentProcessorNumber() + 24));
|
|
|
|
APICWrite(APIC_LDR, tmp);
|
|
|
|
|
|
|
|
|
|
|
|
DPRINT1("CPU%d:\n", CPU);
|
|
|
|
DPRINT1(" Physical APIC id: %d\n", GET_APIC_ID(APICRead(APIC_ID)));
|
|
|
|
DPRINT1(" Logical APIC id: %d\n", GET_APIC_LOGICAL_ID(APICRead(APIC_LDR)));
|
|
|
|
DPRINT1("%08x %08x %08x\n", APICRead(APIC_ID), APICRead(APIC_LDR), APICRead(APIC_DFR));
|
|
|
|
DPRINT1("%d\n", CPUMap[CPU].APICId);
|
|
|
|
|
|
|
|
/* Accept only higher interrupts */
|
|
|
|
APICWrite(APIC_TPR, 0xef);
|
|
|
|
|
|
|
|
/* Enable local APIC */
|
|
|
|
tmp = APICRead(APIC_SIVR);
|
|
|
|
tmp &= ~0xff;
|
|
|
|
tmp |= APIC_SIVR_ENABLE;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
tmp &= ~APIC_SIVR_FOCUS;
|
|
|
|
#else
|
|
|
|
tmp |= APIC_SIVR_FOCUS;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set spurious interrupt vector */
|
|
|
|
tmp |= SPURIOUS_VECTOR;
|
|
|
|
APICWrite(APIC_SIVR, tmp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up LVT0, LVT1:
|
|
|
|
*
|
|
|
|
* set up through-local-APIC on the BP's LINT0. This is not
|
|
|
|
* strictly necessery in pure symmetric-IO mode, but sometimes
|
|
|
|
* we delegate interrupts to the 8259A.
|
|
|
|
*/
|
|
|
|
tmp = APICRead(APIC_LINT0) & APIC_LVT_MASKED;
|
2021-06-11 12:29:21 +00:00
|
|
|
if (CPU == BootCPU && (APICMode == amPIC || !tmp))
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = APIC_DM_EXTINT;
|
|
|
|
DPRINT1("enabled ExtINT on CPU#%d\n", CPU);
|
2021-06-11 12:29:21 +00:00
|
|
|
}
|
|
|
|
else
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = APIC_DM_EXTINT | APIC_LVT_MASKED;
|
|
|
|
DPRINT1("masked ExtINT on CPU#%d\n", CPU);
|
|
|
|
}
|
|
|
|
APICWrite(APIC_LINT0, tmp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only the BSP should see the LINT1 NMI signal, obviously.
|
|
|
|
*/
|
|
|
|
if (CPU == BootCPU)
|
|
|
|
{
|
|
|
|
tmp = APIC_DM_NMI;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = APIC_DM_NMI | APIC_LVT_MASKED;
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
if (!APIC_INTEGRATED(CPUMap[CPU].APICVersion))
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/* 82489DX */
|
|
|
|
tmp |= APIC_LVT_LEVEL_TRIGGER;
|
|
|
|
}
|
|
|
|
APICWrite(APIC_LINT1, tmp);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (APIC_INTEGRATED(CPUMap[CPU].APICVersion))
|
|
|
|
{
|
2004-11-28 01:30:02 +00:00
|
|
|
/* !82489DX */
|
2021-06-11 12:29:21 +00:00
|
|
|
if (APICGetMaxLVT() > 3)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
/* Due to the Pentium erratum 3AP */
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = APICRead(APIC_ESR);
|
|
|
|
DPRINT("ESR value before enabling vector: 0x%X\n", tmp);
|
|
|
|
|
|
|
|
/* Enable sending errors */
|
|
|
|
tmp = ERROR_VECTOR;
|
|
|
|
APICWrite(APIC_LVT3, tmp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Spec says clear errors after enabling vector
|
|
|
|
*/
|
|
|
|
if (APICGetMaxLVT() > 3)
|
|
|
|
{
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
}
|
|
|
|
tmp = APICRead(APIC_ESR);
|
|
|
|
DPRINT("ESR value after enabling vector: 0x%X\n", tmp);
|
|
|
|
}
|
|
|
|
}
|
2005-03-07 16:40:32 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2004-11-28 01:30:02 +00:00
|
|
|
VOID APICSyncArbIDs(VOID)
|
|
|
|
{
|
|
|
|
ULONG i, tmp;
|
|
|
|
|
|
|
|
/* Wait up to 100ms for the APIC to become ready */
|
2021-06-11 12:29:21 +00:00
|
|
|
for (i = 0; i < 10000; i++)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
tmp = APICRead(APIC_ICR0);
|
|
|
|
/* Check Delivery Status */
|
|
|
|
if ((tmp & APIC_ICR0_DS) == 0)
|
|
|
|
break;
|
|
|
|
KeStallExecutionProcessor(10);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (i == 10000)
|
2004-11-28 01:30:02 +00:00
|
|
|
{
|
|
|
|
DPRINT("CPU(%d) APIC busy for 100ms.\n", ThisCPU());
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINT("Synchronizing Arb IDs.\n");
|
|
|
|
APICWrite(APIC_ICR0, APIC_ICR0_DESTS_ALL | APIC_ICR0_LEVEL | APIC_DM_INIT);
|
|
|
|
}
|
2005-03-07 16:40:32 +00:00
|
|
|
#endif
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
VOID MpsErrorHandler(VOID)
|
|
|
|
{
|
|
|
|
ULONG tmp1, tmp2;
|
|
|
|
|
|
|
|
APICDump();
|
|
|
|
|
|
|
|
tmp1 = APICRead(APIC_ESR);
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
tmp2 = APICRead(APIC_ESR);
|
|
|
|
DPRINT1("APIC error on CPU(%d) ESR(%x)(%x)\n", ThisCPU(), tmp1, tmp2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acknowledge the interrupt
|
|
|
|
*/
|
|
|
|
APICSendEOI();
|
|
|
|
|
|
|
|
/* Here is what the APIC error bits mean:
|
|
|
|
* 0: Send CS error
|
|
|
|
* 1: Receive CS error
|
|
|
|
* 2: Send accept error
|
|
|
|
* 3: Receive accept error
|
|
|
|
* 4: Reserved
|
|
|
|
* 5: Send illegal vector
|
|
|
|
* 6: Received illegal vector
|
|
|
|
* 7: Illegal register address
|
|
|
|
*/
|
|
|
|
DPRINT1("APIC error on CPU(%d) ESR(%x)(%x)\n", ThisCPU(), tmp1, tmp2);
|
|
|
|
for (;;);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID MpsSpuriousHandler(VOID)
|
|
|
|
{
|
|
|
|
ULONG tmp;
|
|
|
|
|
2005-01-05 19:01:27 +00:00
|
|
|
DPRINT("Spurious interrupt on CPU(%d)\n", ThisCPU());
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2004-11-28 01:30:02 +00:00
|
|
|
tmp = APICRead(APIC_ISR + ((SPURIOUS_VECTOR & ~0x1f) >> 1));
|
|
|
|
if (tmp & (1 << (SPURIOUS_VECTOR & 0x1f)))
|
|
|
|
{
|
|
|
|
APICSendEOI();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
/* No need to send EOI here */
|
|
|
|
APICDump();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2004-11-28 01:30:02 +00:00
|
|
|
VOID MpsIpiHandler(VOID)
|
|
|
|
{
|
|
|
|
KIRQL oldIrql;
|
|
|
|
|
2006-05-27 15:38:37 +00:00
|
|
|
HalBeginSystemInterrupt(IPI_LEVEL,
|
2021-06-11 12:29:21 +00:00
|
|
|
IPI_VECTOR,
|
2004-11-28 01:30:02 +00:00
|
|
|
&oldIrql);
|
2006-11-17 04:18:41 +00:00
|
|
|
_enable();
|
2004-11-28 01:30:02 +00:00
|
|
|
#if 0
|
2021-06-11 12:29:21 +00:00
|
|
|
DbgPrint("(%s:%d) MpsIpiHandler on CPU%d, current irql is %d\n",
|
2004-11-28 01:30:02 +00:00
|
|
|
__FILE__,__LINE__, KeGetCurrentProcessorNumber(), KeGetCurrentIrql());
|
2021-06-11 12:29:21 +00:00
|
|
|
#endif
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
KiIpiServiceRoutine(NULL, NULL);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
DbgPrint("(%s:%d) MpsIpiHandler on CPU%d done\n", __FILE__,__LINE__, KeGetCurrentProcessorNumber());
|
|
|
|
#endif
|
|
|
|
|
2006-11-17 04:18:41 +00:00
|
|
|
_disable();
|
2004-11-28 01:30:02 +00:00
|
|
|
HalEndSystemInterrupt(oldIrql, 0);
|
|
|
|
}
|
2005-03-07 16:40:32 +00:00
|
|
|
#endif
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
VOID
|
|
|
|
MpsIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
|
|
|
|
PKTRAP_FRAME TrapFrame)
|
|
|
|
{
|
2008-09-04 17:43:00 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
#else
|
2005-11-27 18:24:32 +00:00
|
|
|
TrapFrame->SegGs = (USHORT)IrqTrapFrame->Gs;
|
|
|
|
TrapFrame->SegFs = (USHORT)IrqTrapFrame->Fs;
|
|
|
|
TrapFrame->SegEs = (USHORT)IrqTrapFrame->Es;
|
|
|
|
TrapFrame->SegDs = (USHORT)IrqTrapFrame->Ds;
|
2004-11-28 01:30:02 +00:00
|
|
|
TrapFrame->Eax = IrqTrapFrame->Eax;
|
|
|
|
TrapFrame->Ecx = IrqTrapFrame->Ecx;
|
|
|
|
TrapFrame->Edx = IrqTrapFrame->Edx;
|
|
|
|
TrapFrame->Ebx = IrqTrapFrame->Ebx;
|
2005-11-27 18:24:32 +00:00
|
|
|
TrapFrame->HardwareEsp = IrqTrapFrame->Esp;
|
2004-11-28 01:30:02 +00:00
|
|
|
TrapFrame->Ebp = IrqTrapFrame->Ebp;
|
|
|
|
TrapFrame->Esi = IrqTrapFrame->Esi;
|
|
|
|
TrapFrame->Edi = IrqTrapFrame->Edi;
|
|
|
|
TrapFrame->Eip = IrqTrapFrame->Eip;
|
2005-11-27 18:24:32 +00:00
|
|
|
TrapFrame->SegCs = IrqTrapFrame->Cs;
|
|
|
|
TrapFrame->EFlags = IrqTrapFrame->Eflags;
|
2008-09-04 17:43:00 +00:00
|
|
|
#endif
|
2004-11-28 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
MpsTimerHandler(ULONG Vector, PKIRQ_TRAPFRAME Trapframe)
|
|
|
|
{
|
|
|
|
KIRQL oldIrql;
|
|
|
|
KTRAP_FRAME KernelTrapFrame;
|
|
|
|
#if 0
|
|
|
|
ULONG CPU;
|
|
|
|
static ULONG Count[MAX_CPU] = {0,};
|
|
|
|
#endif
|
2021-06-11 12:29:21 +00:00
|
|
|
HalBeginSystemInterrupt(LOCAL_TIMER_VECTOR,
|
|
|
|
PROFILE_LEVEL,
|
2004-11-28 01:30:02 +00:00
|
|
|
&oldIrql);
|
2006-11-17 04:18:41 +00:00
|
|
|
_enable();
|
2004-11-28 01:30:02 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
CPU = ThisCPU();
|
|
|
|
if ((Count[CPU] % 100) == 0)
|
|
|
|
{
|
2006-05-10 17:47:44 +00:00
|
|
|
DbgPrint("(%s:%d) MpsTimerHandler on CPU%d, irql = %d, epi = %x, KPCR = %x\n", __FILE__, __LINE__, CPU, oldIrql,Trapframe->Eip, KeGetPcr());
|
2004-11-28 01:30:02 +00:00
|
|
|
}
|
|
|
|
Count[CPU]++;
|
|
|
|
#endif
|
|
|
|
|
2006-08-25 00:46:41 +00:00
|
|
|
/* FIXME: SMP is totally broken */
|
2004-11-28 01:30:02 +00:00
|
|
|
MpsIRQTrapFrameToTrapFrame(Trapframe, &KernelTrapFrame);
|
|
|
|
if (KeGetCurrentProcessorNumber() == 0)
|
|
|
|
{
|
2006-08-25 00:46:41 +00:00
|
|
|
//KeUpdateSystemTime(&KernelTrapFrame, oldIrql);
|
2004-11-28 01:30:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-25 00:46:41 +00:00
|
|
|
//KeUpdateRunTime(&KernelTrapFrame, oldIrql);
|
2004-11-28 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 04:18:41 +00:00
|
|
|
_disable();
|
2004-11-28 01:30:02 +00:00
|
|
|
HalEndSystemInterrupt (oldIrql, 0);
|
|
|
|
}
|
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
VOID APICSetupLVTT(ULONG ClockTicks)
|
|
|
|
{
|
|
|
|
ULONG tmp;
|
|
|
|
|
|
|
|
tmp = GET_APIC_VERSION(APICRead(APIC_VER));
|
|
|
|
if (!APIC_INTEGRATED(tmp))
|
|
|
|
{
|
2008-12-25 12:27:47 +00:00
|
|
|
tmp = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) | APIC_LVT_PERIODIC | LOCAL_TIMER_VECTOR;
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Periodic timer */
|
2008-12-25 12:27:47 +00:00
|
|
|
tmp = APIC_LVT_PERIODIC | LOCAL_TIMER_VECTOR;
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
APICWrite(APIC_LVTT, tmp);
|
|
|
|
|
|
|
|
tmp = APICRead(APIC_TDCR);
|
|
|
|
tmp &= ~(APIC_TDCR_1 | APIC_TIMER_BASE_DIV);
|
|
|
|
tmp |= APIC_TDCR_16;
|
|
|
|
APICWrite(APIC_TDCR, tmp);
|
|
|
|
APICWrite(APIC_ICRT, ClockTicks / APIC_DIVISOR);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
VOID
|
2005-03-07 16:40:32 +00:00
|
|
|
APICCalibrateTimer(ULONG CPU)
|
|
|
|
{
|
|
|
|
ULARGE_INTEGER t1, t2;
|
|
|
|
LONG tt1, tt2;
|
|
|
|
BOOLEAN TSCPresent;
|
|
|
|
|
|
|
|
DPRINT("Calibrating APIC timer for CPU %d\n", CPU);
|
|
|
|
|
|
|
|
APICSetupLVTT(1000000000);
|
|
|
|
|
2008-09-04 17:43:00 +00:00
|
|
|
TSCPresent = KeGetCurrentPrcb()->FeatureBits & KF_RDTSC ? TRUE : FALSE;
|
2005-03-07 16:40:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
*/
|
2006-11-29 10:19:00 +00:00
|
|
|
//WaitFor8254Wraparound();
|
2005-03-07 16:40:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We wrapped around just now. Let's start
|
|
|
|
*/
|
|
|
|
if (TSCPresent)
|
|
|
|
{
|
2006-11-17 04:18:41 +00:00
|
|
|
t1.QuadPart = (LONGLONG)__rdtsc();
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
tt1 = APICRead(APIC_CCRT);
|
|
|
|
|
2006-11-29 10:19:00 +00:00
|
|
|
//WaitFor8254Wraparound();
|
2005-03-07 16:40:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
tt2 = APICRead(APIC_CCRT);
|
|
|
|
if (TSCPresent)
|
|
|
|
{
|
2006-11-17 04:18:41 +00:00
|
|
|
t2.QuadPart = (LONGLONG)__rdtsc();
|
2009-09-26 13:41:57 +00:00
|
|
|
CPUMap[CPU].CoreSpeed = (HZ * (ULONG)(t2.QuadPart - t1.QuadPart));
|
2005-03-07 16:40:32 +00:00
|
|
|
DPRINT("CPU clock speed is %ld.%04ld MHz.\n",
|
|
|
|
CPUMap[CPU].CoreSpeed/1000000,
|
|
|
|
CPUMap[CPU].CoreSpeed%1000000);
|
2008-09-04 17:43:00 +00:00
|
|
|
KeGetCurrentPrcb()->MHz = CPUMap[CPU].CoreSpeed/1000000;
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPUMap[CPU].BusSpeed = (HZ * (long)(tt1 - tt2) * APIC_DIVISOR);
|
|
|
|
|
|
|
|
/* Setup timer for normal operation */
|
|
|
|
// APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 100); // 100ns
|
|
|
|
APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 10000); // 10ms
|
|
|
|
// APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 100000); // 100ms
|
|
|
|
|
|
|
|
DPRINT("Host bus clock speed is %ld.%04ld MHz.\n",
|
|
|
|
CPUMap[CPU].BusSpeed/1000000,
|
|
|
|
CPUMap[CPU].BusSpeed%1000000);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
VOID
|
2008-09-04 17:43:00 +00:00
|
|
|
SetInterruptGate(ULONG index, ULONG_PTR address)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
2008-09-04 17:43:00 +00:00
|
|
|
#ifdef _M_AMD64
|
Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
35905, 35906, 35942, 35947, 35948, 35949, 35953, 36013, 36388, 36389, 36570, 37873, 37991, 39151 from amd64 branch
svn path=/trunk/; revision=43173
2009-09-26 21:32:53 +00:00
|
|
|
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;
|
2008-09-04 17:43:00 +00:00
|
|
|
#else
|
2005-06-29 18:33:39 +00:00
|
|
|
KIDTENTRY *idt;
|
2005-06-29 19:06:28 +00:00
|
|
|
KIDT_ACCESS Access;
|
|
|
|
|
|
|
|
/* Set the IDT Access Bits */
|
|
|
|
Access.Reserved = 0;
|
|
|
|
Access.Present = 1;
|
|
|
|
Access.Dpl = 0; /* Kernel-Mode */
|
|
|
|
Access.SystemSegmentFlag = 0;
|
|
|
|
Access.SegmentType = I386_INTERRUPT_GATE;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2006-05-10 17:47:44 +00:00
|
|
|
idt = (KIDTENTRY*)((ULONG)KeGetPcr()->IDT + index * sizeof(KIDTENTRY));
|
2009-09-26 13:41:57 +00:00
|
|
|
idt->Offset = (USHORT)(address & 0xffff);
|
2005-11-27 03:08:35 +00:00
|
|
|
idt->Selector = KGDT_R0_CODE;
|
2005-06-29 19:06:28 +00:00
|
|
|
idt->Access = Access.Value;
|
2009-09-26 13:41:57 +00:00
|
|
|
idt->ExtendedOffset = (USHORT)(address >> 16);
|
2008-09-04 17:43:00 +00:00
|
|
|
#endif
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID HaliInitBSP(VOID)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
PUSHORT ps;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static BOOLEAN BSPInitialized = FALSE;
|
|
|
|
|
|
|
|
/* Only initialize the BSP once */
|
|
|
|
if (BSPInitialized)
|
|
|
|
{
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2005-03-07 16:40:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BSPInitialized = TRUE;
|
|
|
|
|
|
|
|
/* Setup interrupt handlers */
|
2008-09-04 17:43:00 +00:00
|
|
|
SetInterruptGate(LOCAL_TIMER_VECTOR, (ULONG_PTR)MpsTimerInterrupt);
|
|
|
|
SetInterruptGate(ERROR_VECTOR, (ULONG_PTR)MpsErrorInterrupt);
|
|
|
|
SetInterruptGate(SPURIOUS_VECTOR, (ULONG_PTR)MpsSpuriousInterrupt);
|
2005-03-07 16:40:32 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2008-09-04 17:43:00 +00:00
|
|
|
SetInterruptGate(IPI_VECTOR, (ULONG_PTR)MpsIpiInterrupt);
|
2005-03-07 16:40:32 +00:00
|
|
|
#endif
|
|
|
|
DPRINT("APIC is mapped at 0x%X\n", APICBase);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (VerifyLocalAPIC())
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DPRINT("APIC found\n");
|
2021-06-11 12:29:21 +00:00
|
|
|
}
|
|
|
|
else
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DPRINT("No APIC found\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (APICMode == amPIC)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
EnableApicMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
APICSetup();
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* BIOS data segment */
|
|
|
|
BIOSBase = (PULONG)BIOS_AREA;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
/* Area for communicating with the APs */
|
|
|
|
CommonBase = (PULONG)COMMON_AREA;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
/* Copy bootstrap code to common area */
|
Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
35905, 35906, 35942, 35947, 35948, 35949, 35953, 36013, 36388, 36389, 36570, 37873, 37991, 39151 from amd64 branch
svn path=/trunk/; revision=43173
2009-09-26 21:32:53 +00:00
|
|
|
memcpy((PVOID)((ULONG_PTR)CommonBase + PAGE_SIZE),
|
2005-03-07 16:40:32 +00:00
|
|
|
&APstart,
|
Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
35905, 35906, 35942, 35947, 35948, 35949, 35953, 36013, 36388, 36389, 36570, 37873, 37991, 39151 from amd64 branch
svn path=/trunk/; revision=43173
2009-09-26 21:32:53 +00:00
|
|
|
(ULONG_PTR)&APend - (ULONG_PTR)&APstart + 1);
|
2005-03-07 16:40:32 +00:00
|
|
|
|
|
|
|
/* Set shutdown code */
|
|
|
|
CMOS_WRITE(0xF, 0xA);
|
|
|
|
|
|
|
|
/* Set warm reset vector */
|
Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
35905, 35906, 35942, 35947, 35948, 35949, 35953, 36013, 36388, 36389, 36570, 37873, 37991, 39151 from amd64 branch
svn path=/trunk/; revision=43173
2009-09-26 21:32:53 +00:00
|
|
|
ps = (PUSHORT)((ULONG_PTR)BIOSBase + 0x467);
|
2005-03-07 16:40:32 +00:00
|
|
|
*ps = (COMMON_AREA + PAGE_SIZE) & 0xF;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
35905, 35906, 35942, 35947, 35948, 35949, 35953, 36013, 36388, 36389, 36570, 37873, 37991, 39151 from amd64 branch
svn path=/trunk/; revision=43173
2009-09-26 21:32:53 +00:00
|
|
|
ps = (PUSHORT)((ULONG_PTR)BIOSBase + 0x469);
|
2005-03-07 16:40:32 +00:00
|
|
|
*ps = (COMMON_AREA + PAGE_SIZE) >> 4;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Calibrate APIC timer */
|
|
|
|
APICCalibrateTimer(BootCPU);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
VOID
|
|
|
|
HaliStartApplicationProcessor(ULONG Cpu, ULONG Stack)
|
|
|
|
{
|
|
|
|
ULONG tmp, maxlvt;
|
|
|
|
PCOMMON_AREA_INFO Common;
|
|
|
|
ULONG StartupCount;
|
|
|
|
ULONG i, j;
|
|
|
|
ULONG DeliveryStatus = 0;
|
|
|
|
ULONG AcceptStatus = 0;
|
|
|
|
|
|
|
|
if (Cpu >= MAX_CPU ||
|
|
|
|
Cpu >= CPUCount ||
|
|
|
|
OnlineCPUs & (1 << Cpu))
|
|
|
|
{
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2005-03-07 16:40:32 +00:00
|
|
|
}
|
|
|
|
DPRINT1("Attempting to boot CPU %d\n", Cpu);
|
|
|
|
|
|
|
|
/* Send INIT IPI */
|
|
|
|
|
|
|
|
APICSendIPI(Cpu, APIC_DM_INIT|APIC_ICR0_LEVEL_ASSERT);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2005-03-07 16:40:32 +00:00
|
|
|
KeStallExecutionProcessor(200);
|
|
|
|
|
|
|
|
/* Deassert INIT */
|
|
|
|
|
|
|
|
APICSendIPI(Cpu, APIC_DM_INIT|APIC_ICR0_LEVEL_DEASSERT);
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (APIC_INTEGRATED(CPUMap[Cpu].APICVersion))
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
/* Clear APIC errors */
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
tmp = (APICRead(APIC_ESR) & APIC_ESR_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common = (PCOMMON_AREA_INFO)CommonBase;
|
|
|
|
|
|
|
|
/* Write the location of the AP stack */
|
|
|
|
Common->Stack = (ULONG)Stack;
|
|
|
|
/* Write the page directory page */
|
2006-05-10 17:47:44 +00:00
|
|
|
Common->PageDirectory = __readcr3();
|
2005-03-07 16:40:32 +00:00
|
|
|
/* Write the kernel entry point */
|
2005-07-16 13:01:18 +00:00
|
|
|
Common->NtProcessStartup = (ULONG_PTR)RtlImageNtHeader((PVOID)KernelBase)->OptionalHeader.AddressOfEntryPoint + KernelBase;
|
2005-03-07 16:40:32 +00:00
|
|
|
/* Write the state of the mae mode */
|
2006-05-10 17:47:44 +00:00
|
|
|
Common->PaeModeEnabled = __readcr4() & CR4_PAE ? 1 : 0;
|
2005-03-07 16:40:32 +00:00
|
|
|
|
|
|
|
DPRINT1("%x %x %x %x\n", Common->Stack, Common->PageDirectory, Common->NtProcessStartup, Common->PaeModeEnabled);
|
|
|
|
|
|
|
|
DPRINT("Cpu %d got stack at 0x%X\n", Cpu, Common->Stack);
|
|
|
|
#if 0
|
2021-06-11 12:29:21 +00:00
|
|
|
for (j = 0; j < 16; j++)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
Common->Debug[j] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
maxlvt = APICGetMaxLVT();
|
|
|
|
|
|
|
|
/* Is this a local APIC or an 82489DX? */
|
|
|
|
StartupCount = (APIC_INTEGRATED(CPUMap[Cpu].APICVersion)) ? 2 : 0;
|
|
|
|
|
|
|
|
for (i = 1; i <= StartupCount; i++)
|
|
|
|
{
|
|
|
|
/* It's a local APIC, so send STARTUP IPI */
|
|
|
|
DPRINT("Sending startup signal %d\n", i);
|
|
|
|
/* Clear errors */
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
APICRead(APIC_ESR);
|
|
|
|
|
|
|
|
APICSendIPI(Cpu, APIC_DM_STARTUP | ((COMMON_AREA + PAGE_SIZE) >> 12)|APIC_ICR0_LEVEL_DEASSERT);
|
|
|
|
|
|
|
|
/* Wait up to 10ms for IPI to be delivered */
|
|
|
|
j = 0;
|
2021-06-11 12:29:21 +00:00
|
|
|
do
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
KeStallExecutionProcessor(10);
|
|
|
|
|
|
|
|
/* Check Delivery Status */
|
|
|
|
DeliveryStatus = APICRead(APIC_ICR0) & APIC_ICR0_DS;
|
|
|
|
|
|
|
|
j++;
|
|
|
|
} while ((DeliveryStatus) && (j < 1000));
|
|
|
|
|
|
|
|
KeStallExecutionProcessor(200);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Due to the Pentium erratum 3AP.
|
|
|
|
*/
|
2021-06-11 12:29:21 +00:00
|
|
|
if (maxlvt > 3)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
APICRead(APIC_SIVR);
|
|
|
|
APICWrite(APIC_ESR, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AcceptStatus = APICRead(APIC_ESR) & APIC_ESR_MASK;
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (DeliveryStatus || AcceptStatus)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (DeliveryStatus)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DPRINT("STARTUP IPI for CPU %d was never delivered.\n", Cpu);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (AcceptStatus)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DPRINT("STARTUP IPI for CPU %d was never accepted.\n", Cpu);
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (!(DeliveryStatus || AcceptStatus))
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Wait no more than 5 seconds for processor to boot */
|
|
|
|
DPRINT("Waiting for 5 seconds for CPU %d to boot\n", Cpu);
|
|
|
|
|
|
|
|
/* Wait no more than 5 seconds */
|
2021-06-11 12:29:21 +00:00
|
|
|
for (j = 0; j < 50000; j++)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
if (CPUMap[Cpu].Flags & CPU_ENABLED)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
KeStallExecutionProcessor(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
if (CPUMap[Cpu].Flags & CPU_ENABLED)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DbgPrint("CPU %d is now running\n", Cpu);
|
2021-06-11 12:29:21 +00:00
|
|
|
}
|
|
|
|
else
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DbgPrint("Initialization of CPU %d failed\n", Cpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
DPRINT("Debug bytes are:\n");
|
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
for (j = 0; j < 4; j++)
|
2005-03-07 16:40:32 +00:00
|
|
|
{
|
|
|
|
DPRINT("0x%08X 0x%08X 0x%08X 0x%08X.\n",
|
|
|
|
Common->Debug[j*4+0],
|
|
|
|
Common->Debug[j*4+1],
|
|
|
|
Common->Debug[j*4+2],
|
|
|
|
Common->Debug[j*4+3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
DECLSPEC_NORETURN
|
|
|
|
HalpApcInterruptHandler(IN PKTRAP_FRAME TrapFrame)
|
|
|
|
{
|
|
|
|
/* Set up a fake INT Stack */
|
|
|
|
TrapFrame->EFlags = __readeflags();
|
|
|
|
TrapFrame->SegCs = KGDT_R0_CODE;
|
|
|
|
TrapFrame->Eip = TrapFrame->Eax;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
/* Build the trap frame */
|
|
|
|
KiEnterInterruptTrap(TrapFrame);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
/* unimplemented */
|
|
|
|
UNIMPLEMENTED;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
/* Exit the interrupt */
|
2021-06-11 12:29:21 +00:00
|
|
|
KiEoiHelper(TrapFrame);
|
2010-02-10 23:24:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-11-28 01:30:02 +00:00
|
|
|
/* EOF */
|