X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FCpuDxe%2FCpuDxe.c;h=6ae2dcd1c71136bd6fa364ba906dd80a6befcde6;hp=2dfde0677d88b54d5988bf974bbfa5f853dbacdc;hb=147fd35c3e389ecd025dbfd243312bf5b22da7c9;hpb=79aca636ab2f4c4b4c5b80c94ec04f0219aef7b0 diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c index 2dfde0677d..6ae2dcd1c7 100644 --- a/UefiCpuPkg/CpuDxe/CpuDxe.c +++ b/UefiCpuPkg/CpuDxe/CpuDxe.c @@ -1,7 +1,7 @@ /** @file - CPU DXE Module. + CPU DXE Module to produce CPU ARCH Protocol. - Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.
+ Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -13,74 +13,76 @@ **/ #include "CpuDxe.h" +#include "CpuMp.h" +#include "CpuPageTable.h" + +#define CACHE_ATTRIBUTE_MASK (EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_WP) +#define MEMORY_ATTRIBUTE_MASK (EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_RO) // // Global Variables // -IA32_IDT_GATE_DESCRIPTOR gIdtTable[INTERRUPT_VECTOR_NUMBER] = { { { 0 } } }; - -EFI_CPU_INTERRUPT_HANDLER ExternalVectorTable[0x100]; BOOLEAN InterruptState = FALSE; EFI_HANDLE mCpuHandle = NULL; BOOLEAN mIsFlushingGCD; -UINT64 mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS; -UINT64 mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK; -IA32_IDT_GATE_DESCRIPTOR *mOrigIdtEntry = NULL; -UINT16 mOrigIdtEntryCount = 0; +BOOLEAN mIsAllocatingPageTable = FALSE; +UINT64 mValidMtrrAddressMask; +UINT64 mValidMtrrBitsMask; +UINT64 mTimerPeriod = 0; FIXED_MTRR mFixedMtrrTable[] = { { - MTRR_LIB_IA32_MTRR_FIX64K_00000, + MSR_IA32_MTRR_FIX64K_00000, 0, 0x10000 }, { - MTRR_LIB_IA32_MTRR_FIX16K_80000, + MSR_IA32_MTRR_FIX16K_80000, 0x80000, 0x4000 }, { - MTRR_LIB_IA32_MTRR_FIX16K_A0000, + MSR_IA32_MTRR_FIX16K_A0000, 0xA0000, 0x4000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_C0000, + MSR_IA32_MTRR_FIX4K_C0000, 0xC0000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_C8000, + MSR_IA32_MTRR_FIX4K_C8000, 0xC8000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_D0000, + MSR_IA32_MTRR_FIX4K_D0000, 0xD0000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_D8000, + MSR_IA32_MTRR_FIX4K_D8000, 0xD8000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_E0000, + MSR_IA32_MTRR_FIX4K_E0000, 0xE0000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_E8000, + MSR_IA32_MTRR_FIX4K_E8000, 0xE8000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_F0000, + MSR_IA32_MTRR_FIX4K_F0000, 0xF0000, 0x1000 }, { - MTRR_LIB_IA32_MTRR_FIX4K_F8000, + MSR_IA32_MTRR_FIX4K_F8000, 0xF8000, 0x1000 }, @@ -100,259 +102,10 @@ EFI_CPU_ARCH_PROTOCOL gCpu = { 4 // DmaBufferAlignment }; -// -// Error code flag indicating whether or not an error code will be -// pushed on the stack if an exception occurs. -// -// 1 means an error code will be pushed, otherwise 0 -// -// bit 0 - exception 0 -// bit 1 - exception 1 -// etc. -// -UINT32 mErrorCodeFlag = 0x00027d00; - -// -// Local function prototypes -// - -/** - Set Interrupt Descriptor Table Handler Address. - - @param Index The Index of the interrupt descriptor table handle. - @param Handler Handler address. - -**/ -VOID -SetInterruptDescriptorTableHandlerAddress ( - IN UINTN Index, - IN VOID *Handler OPTIONAL - ); - // // CPU Arch Protocol Functions // - -/** - Common exception handler. - - @param InterruptType Exception type - @param SystemContext EFI_SYSTEM_CONTEXT - -**/ -VOID -EFIAPI -CommonExceptionHandler ( - IN EFI_EXCEPTION_TYPE InterruptType, - IN EFI_SYSTEM_CONTEXT SystemContext - ) -{ -#if defined (MDE_CPU_IA32) - DEBUG (( - EFI_D_ERROR, - "!!!! IA32 Exception Type - %08x !!!!\n", - InterruptType - )); - if ((mErrorCodeFlag & (1 << InterruptType)) != 0) { - DEBUG (( - EFI_D_ERROR, - "ExceptionData - %08x\n", - SystemContext.SystemContextIa32->ExceptionData - )); - } - DEBUG (( - EFI_D_ERROR, - "CS - %04x, EIP - %08x, EFL - %08x, SS - %04x\n", - SystemContext.SystemContextIa32->Cs, - SystemContext.SystemContextIa32->Eip, - SystemContext.SystemContextIa32->Eflags, - SystemContext.SystemContextIa32->Ss - )); - DEBUG (( - EFI_D_ERROR, - "DS - %04x, ES - %04x, FS - %04x, GS - %04x\n", - SystemContext.SystemContextIa32->Ds, - SystemContext.SystemContextIa32->Es, - SystemContext.SystemContextIa32->Fs, - SystemContext.SystemContextIa32->Gs - )); - DEBUG (( - EFI_D_ERROR, - "EAX - %08x, EBX - %08x, ECX - %08x, EDX - %08x\n", - SystemContext.SystemContextIa32->Eax, - SystemContext.SystemContextIa32->Ebx, - SystemContext.SystemContextIa32->Ecx, - SystemContext.SystemContextIa32->Edx - )); - DEBUG (( - EFI_D_ERROR, - "ESP - %08x, EBP - %08x, ESI - %08x, EDI - %08x\n", - SystemContext.SystemContextIa32->Esp, - SystemContext.SystemContextIa32->Ebp, - SystemContext.SystemContextIa32->Esi, - SystemContext.SystemContextIa32->Edi - )); - DEBUG (( - EFI_D_ERROR, - "GDT - %08x LIM - %04x, IDT - %08x LIM - %04x\n", - SystemContext.SystemContextIa32->Gdtr[0], - SystemContext.SystemContextIa32->Gdtr[1], - SystemContext.SystemContextIa32->Idtr[0], - SystemContext.SystemContextIa32->Idtr[1] - )); - DEBUG (( - EFI_D_ERROR, - "LDT - %08x, TR - %08x\n", - SystemContext.SystemContextIa32->Ldtr, - SystemContext.SystemContextIa32->Tr - )); - DEBUG (( - EFI_D_ERROR, - "CR0 - %08x, CR2 - %08x, CR3 - %08x, CR4 - %08x\n", - SystemContext.SystemContextIa32->Cr0, - SystemContext.SystemContextIa32->Cr2, - SystemContext.SystemContextIa32->Cr3, - SystemContext.SystemContextIa32->Cr4 - )); - DEBUG (( - EFI_D_ERROR, - "DR0 - %08x, DR1 - %08x, DR2 - %08x, DR3 - %08x\n", - SystemContext.SystemContextIa32->Dr0, - SystemContext.SystemContextIa32->Dr1, - SystemContext.SystemContextIa32->Dr2, - SystemContext.SystemContextIa32->Dr3 - )); - DEBUG (( - EFI_D_ERROR, - "DR6 - %08x, DR7 - %08x\n", - SystemContext.SystemContextIa32->Dr6, - SystemContext.SystemContextIa32->Dr7 - )); -#elif defined (MDE_CPU_X64) - DEBUG (( - EFI_D_ERROR, - "!!!! X64 Exception Type - %016lx !!!!\n", - (UINT64)InterruptType - )); - if ((mErrorCodeFlag & (1 << InterruptType)) != 0) { - DEBUG (( - EFI_D_ERROR, - "ExceptionData - %016lx\n", - SystemContext.SystemContextX64->ExceptionData - )); - } - DEBUG (( - EFI_D_ERROR, - "RIP - %016lx, RFL - %016lx\n", - SystemContext.SystemContextX64->Rip, - SystemContext.SystemContextX64->Rflags - )); - DEBUG (( - EFI_D_ERROR, - "RAX - %016lx, RCX - %016lx, RDX - %016lx\n", - SystemContext.SystemContextX64->Rax, - SystemContext.SystemContextX64->Rcx, - SystemContext.SystemContextX64->Rdx - )); - DEBUG (( - EFI_D_ERROR, - "RBX - %016lx, RSP - %016lx, RBP - %016lx\n", - SystemContext.SystemContextX64->Rbx, - SystemContext.SystemContextX64->Rsp, - SystemContext.SystemContextX64->Rbp - )); - DEBUG (( - EFI_D_ERROR, - "RSI - %016lx, RDI - %016lx\n", - SystemContext.SystemContextX64->Rsi, - SystemContext.SystemContextX64->Rdi - )); - DEBUG (( - EFI_D_ERROR, - "R8 - %016lx, R9 - %016lx, R10 - %016lx\n", - SystemContext.SystemContextX64->R8, - SystemContext.SystemContextX64->R9, - SystemContext.SystemContextX64->R10 - )); - DEBUG (( - EFI_D_ERROR, - "R11 - %016lx, R12 - %016lx, R13 - %016lx\n", - SystemContext.SystemContextX64->R11, - SystemContext.SystemContextX64->R12, - SystemContext.SystemContextX64->R13 - )); - DEBUG (( - EFI_D_ERROR, - "R14 - %016lx, R15 - %016lx\n", - SystemContext.SystemContextX64->R14, - SystemContext.SystemContextX64->R15 - )); - DEBUG (( - EFI_D_ERROR, - "CS - %04lx, DS - %04lx, ES - %04lx, FS - %04lx, GS - %04lx, SS - %04lx\n", - SystemContext.SystemContextX64->Cs, - SystemContext.SystemContextX64->Ds, - SystemContext.SystemContextX64->Es, - SystemContext.SystemContextX64->Fs, - SystemContext.SystemContextX64->Gs, - SystemContext.SystemContextX64->Ss - )); - DEBUG (( - EFI_D_ERROR, - "GDT - %016lx; %04lx, IDT - %016lx; %04lx\n", - SystemContext.SystemContextX64->Gdtr[0], - SystemContext.SystemContextX64->Gdtr[1], - SystemContext.SystemContextX64->Idtr[0], - SystemContext.SystemContextX64->Idtr[1] - )); - DEBUG (( - EFI_D_ERROR, - "LDT - %016lx, TR - %016lx\n", - SystemContext.SystemContextX64->Ldtr, - SystemContext.SystemContextX64->Tr - )); - DEBUG (( - EFI_D_ERROR, - "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n", - SystemContext.SystemContextX64->Cr0, - SystemContext.SystemContextX64->Cr2, - SystemContext.SystemContextX64->Cr3 - )); - DEBUG (( - EFI_D_ERROR, - "CR4 - %016lx, CR8 - %016lx\n", - SystemContext.SystemContextX64->Cr4, - SystemContext.SystemContextX64->Cr8 - )); - DEBUG (( - EFI_D_ERROR, - "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n", - SystemContext.SystemContextX64->Dr0, - SystemContext.SystemContextX64->Dr1, - SystemContext.SystemContextX64->Dr2 - )); - DEBUG (( - EFI_D_ERROR, - "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n", - SystemContext.SystemContextX64->Dr3, - SystemContext.SystemContextX64->Dr6, - SystemContext.SystemContextX64->Dr7 - )); -#else -#error CPU type not supported for exception information dump! -#endif - - // - // Hang the system with CpuSleep so the processor will enter a lower power - // state. - // - while (TRUE) { - CpuSleep (); - }; -} - - /** Flush CPU data cache. If the instruction cache is fully coherent with all DMA operations then function can just return EFI_SUCCESS. @@ -510,29 +263,7 @@ CpuRegisterInterruptHandler ( IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler ) { - if (InterruptType < 0 || InterruptType > 0xff) { - return EFI_UNSUPPORTED; - } - - if (InterruptHandler == NULL && ExternalVectorTable[InterruptType] == NULL) { - return EFI_INVALID_PARAMETER; - } - - if (InterruptHandler != NULL && ExternalVectorTable[InterruptType] != NULL) { - return EFI_ALREADY_STARTED; - } - - if (InterruptHandler != NULL) { - SetInterruptDescriptorTableHandlerAddress ((UINTN)InterruptType, NULL); - } else { - // - // Restore the original IDT handler address if InterruptHandler is NULL. - // - RestoreInterruptDescriptorTableHandlerAddress ((UINTN)InterruptType); - } - - ExternalVectorTable[InterruptType] = InterruptHandler; - return EFI_SUCCESS; + return RegisterCpuInterruptHandler (InterruptType, InterruptHandler); } @@ -568,6 +299,9 @@ CpuGetTimerValue ( OUT UINT64 *TimerPeriod OPTIONAL ) { + UINT64 BeginValue; + UINT64 EndValue; + if (TimerValue == NULL) { return EFI_INVALID_PARAMETER; } @@ -579,15 +313,46 @@ CpuGetTimerValue ( *TimerValue = AsmReadTsc (); if (TimerPeriod != NULL) { + if (mTimerPeriod == 0) { // - // BugBug: Hard coded. Don't know how to do this generically + // Read time stamp counter before and after delay of 100 microseconds // - *TimerPeriod = 1000000000; + BeginValue = AsmReadTsc (); + MicroSecondDelay (100); + EndValue = AsmReadTsc (); + // + // Calculate the actual frequency + // + mTimerPeriod = DivU64x64Remainder ( + MultU64x32 ( + 1000 * 1000 * 1000, + 100 + ), + EndValue - BeginValue, + NULL + ); + } + *TimerPeriod = mTimerPeriod; } return EFI_SUCCESS; } +/** + A minimal wrapper function that allows MtrrSetAllMtrrs() to be passed to + EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() as Procedure. + + @param[in] Buffer Pointer to an MTRR_SETTINGS object, to be passed to + MtrrSetAllMtrrs(). +**/ +VOID +EFIAPI +SetMtrrsFromBuffer ( + IN VOID *Buffer + ) +{ + MtrrSetAllMtrrs (Buffer); +} /** Implementation of SetMemoryAttributes() service of CPU Architecture Protocol. @@ -625,10 +390,12 @@ CpuSetMemoryAttributes ( { RETURN_STATUS Status; MTRR_MEMORY_CACHE_TYPE CacheType; - - if (!IsMtrrSupported ()) { - return EFI_UNSUPPORTED; - } + EFI_STATUS MpStatus; + EFI_MP_SERVICES_PROTOCOL *MpService; + MTRR_SETTINGS MtrrSettings; + UINT64 CacheAttributes; + UINT64 MemoryAttributes; + MTRR_MEMORY_CACHE_TYPE CurrentCacheType; // // If this function is called because GCD SetMemorySpaceAttributes () is called @@ -637,50 +404,105 @@ CpuSetMemoryAttributes ( // to avoid unnecessary computing. // if (mIsFlushingGCD) { - DEBUG((EFI_D_INFO, " Flushing GCD\n")); + DEBUG((DEBUG_INFO, " Flushing GCD\n")); return EFI_SUCCESS; } - switch (Attributes) { - case EFI_MEMORY_UC: - CacheType = CacheUncacheable; - break; + // + // During memory attributes updating, new pages may be allocated to setup + // smaller granularity of page table. Page allocation action might then cause + // another calling of CpuSetMemoryAttributes() recursively, due to memory + // protection policy configured (such as PcdDxeNxMemoryProtectionPolicy). + // Since this driver will always protect memory used as page table by itself, + // there's no need to apply protection policy requested from memory service. + // So it's safe to just return EFI_SUCCESS if this time of calling is caused + // by page table memory allocation. + // + if (mIsAllocatingPageTable) { + DEBUG((DEBUG_VERBOSE, " Allocating page table memory\n")); + return EFI_SUCCESS; + } - case EFI_MEMORY_WC: - CacheType = CacheWriteCombining; - break; + CacheAttributes = Attributes & CACHE_ATTRIBUTE_MASK; + MemoryAttributes = Attributes & MEMORY_ATTRIBUTE_MASK; - case EFI_MEMORY_WT: - CacheType = CacheWriteThrough; - break; + if (Attributes != (CacheAttributes | MemoryAttributes)) { + return EFI_INVALID_PARAMETER; + } - case EFI_MEMORY_WP: - CacheType = CacheWriteProtected; - break; + if (CacheAttributes != 0) { + if (!IsMtrrSupported ()) { + return EFI_UNSUPPORTED; + } - case EFI_MEMORY_WB: - CacheType = CacheWriteBack; - break; + switch (CacheAttributes) { + case EFI_MEMORY_UC: + CacheType = CacheUncacheable; + break; - case EFI_MEMORY_UCE: - case EFI_MEMORY_RP: - case EFI_MEMORY_XP: - case EFI_MEMORY_RUNTIME: - return EFI_UNSUPPORTED; + case EFI_MEMORY_WC: + CacheType = CacheWriteCombining; + break; - default: - return EFI_INVALID_PARAMETER; + case EFI_MEMORY_WT: + CacheType = CacheWriteThrough; + break; + + case EFI_MEMORY_WP: + CacheType = CacheWriteProtected; + break; + + case EFI_MEMORY_WB: + CacheType = CacheWriteBack; + break; + + default: + return EFI_INVALID_PARAMETER; + } + CurrentCacheType = MtrrGetMemoryAttribute(BaseAddress); + if (CurrentCacheType != CacheType) { + // + // call MTRR libary function + // + Status = MtrrSetMemoryAttribute ( + BaseAddress, + Length, + CacheType + ); + + if (!RETURN_ERROR (Status)) { + MpStatus = gBS->LocateProtocol ( + &gEfiMpServiceProtocolGuid, + NULL, + (VOID **)&MpService + ); + // + // Synchronize the update with all APs + // + if (!EFI_ERROR (MpStatus)) { + MtrrGetAllMtrrs (&MtrrSettings); + MpStatus = MpService->StartupAllAPs ( + MpService, // This + SetMtrrsFromBuffer, // Procedure + FALSE, // SingleThread + NULL, // WaitEvent + 0, // TimeoutInMicrosecsond + &MtrrSettings, // ProcedureArgument + NULL // FailedCpuList + ); + ASSERT (MpStatus == EFI_SUCCESS || MpStatus == EFI_NOT_STARTED); + } + } + if (EFI_ERROR(Status)) { + return Status; + } + } } + // - // call MTRR libary function + // Set memory attribute by page table // - Status = MtrrSetMemoryAttribute ( - BaseAddress, - Length, - CacheType - ); - - return (EFI_STATUS) Status; + return AssignMemoryPageAttributes (NULL, BaseAddress, Length, MemoryAttributes, NULL); } /** @@ -703,13 +525,12 @@ InitializeMtrrMask ( AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL); PhysicalAddressBits = (UINT8) RegEax; - - mValidMtrrBitsMask = LShiftU64 (1, PhysicalAddressBits) - 1; - mValidMtrrAddressMask = mValidMtrrBitsMask & 0xfffffffffffff000ULL; } else { - mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK; - mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS; + PhysicalAddressBits = 36; } + + mValidMtrrBitsMask = LShiftU64 (1, PhysicalAddressBits) - 1; + mValidMtrrAddressMask = mValidMtrrBitsMask & 0xfffffffffffff000ULL; } /** @@ -877,7 +698,7 @@ SetGcdMemorySpaceAttributes ( **/ VOID -RefreshGcdMemoryAttributes ( +RefreshMemoryAttributesFromMtrr ( VOID ) { @@ -898,14 +719,9 @@ RefreshGcdMemoryAttributes ( UINT32 FirmwareVariableMtrrCount; UINT8 DefaultMemoryType; - if (!IsMtrrSupported ()) { - return; - } - FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); ASSERT (FirmwareVariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); - mIsFlushingGCD = TRUE; MemorySpaceMap = NULL; // @@ -1056,175 +872,277 @@ RefreshGcdMemoryAttributes ( if (MemorySpaceMap != NULL) { FreePool (MemorySpaceMap); } +} - mIsFlushingGCD = FALSE; +/** + Check if paging is enabled or not. +**/ +BOOLEAN +IsPagingAndPageAddressExtensionsEnabled ( + VOID + ) +{ + IA32_CR0 Cr0; + IA32_CR4 Cr4; + + Cr0.UintN = AsmReadCr0 (); + Cr4.UintN = AsmReadCr4 (); + + return ((Cr0.Bits.PG != 0) && (Cr4.Bits.PAE != 0)); } /** - Set Interrupt Descriptor Table Handler Address. + Refreshes the GCD Memory Space attributes according to MTRRs and Paging. - @param Index The Index of the interrupt descriptor table handle. - @param Handler Handler address. + This function refreshes the GCD Memory Space attributes according to MTRRs + and page tables. **/ VOID -SetInterruptDescriptorTableHandlerAddress ( - IN UINTN Index, - IN VOID *Handler OPTIONAL +RefreshGcdMemoryAttributes ( + VOID ) { - UINTN UintnHandler; + mIsFlushingGCD = TRUE; - if (Handler != NULL) { - UintnHandler = (UINTN) Handler; - } else { - UintnHandler = ((UINTN) AsmIdtVector00) + (8 * Index); + if (IsMtrrSupported ()) { + RefreshMemoryAttributesFromMtrr (); } - gIdtTable[Index].Bits.OffsetLow = (UINT16)UintnHandler; - gIdtTable[Index].Bits.Reserved_0 = 0; - gIdtTable[Index].Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32; - gIdtTable[Index].Bits.OffsetHigh = (UINT16)(UintnHandler >> 16); -#if defined (MDE_CPU_X64) - gIdtTable[Index].Bits.OffsetUpper = (UINT32)(UintnHandler >> 32); - gIdtTable[Index].Bits.Reserved_1 = 0; -#endif + if (IsPagingAndPageAddressExtensionsEnabled ()) { + RefreshGcdMemoryAttributesFromPaging (); + } + + mIsFlushingGCD = FALSE; } /** - Restore original Interrupt Descriptor Table Handler Address. - - @param Index The Index of the interrupt descriptor table handle. + Initialize Interrupt Descriptor Table for interrupt handling. **/ VOID -RestoreInterruptDescriptorTableHandlerAddress ( - IN UINTN Index +InitInterruptDescriptorTable ( + VOID ) { - if (Index < mOrigIdtEntryCount) { - gIdtTable[Index].Bits.OffsetLow = mOrigIdtEntry[Index].Bits.OffsetLow; - gIdtTable[Index].Bits.OffsetHigh = mOrigIdtEntry[Index].Bits.OffsetHigh; -#if defined (MDE_CPU_X64) - gIdtTable[Index].Bits.OffsetUpper = mOrigIdtEntry[Index].Bits.OffsetUpper; -#endif + EFI_STATUS Status; + EFI_VECTOR_HANDOFF_INFO *VectorInfoList; + EFI_VECTOR_HANDOFF_INFO *VectorInfo; + + VectorInfo = NULL; + Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid, (VOID **) &VectorInfoList); + if (Status == EFI_SUCCESS && VectorInfoList != NULL) { + VectorInfo = VectorInfoList; } + Status = InitializeCpuInterruptHandlers (VectorInfo); + ASSERT_EFI_ERROR (Status); } + /** - Initialize Interrupt Descriptor Table for interrupt handling. + Callback function for idle events. + + @param Event Event whose notification function is being invoked. + @param Context The pointer to the notification function's context, + which is implementation-dependent. **/ VOID -InitInterruptDescriptorTable ( - VOID +EFIAPI +IdleLoopEventCallback ( + IN EFI_EVENT Event, + IN VOID *Context ) { - EFI_STATUS Status; - IA32_DESCRIPTOR OldIdtPtr; - IA32_IDT_GATE_DESCRIPTOR *OldIdt; - UINTN OldIdtSize; - VOID *IdtPtrAlignmentBuffer; - IA32_DESCRIPTOR *IdtPtr; - UINTN Index; - UINT16 CurrentCs; - VOID *IntHandler; - - SetMem (ExternalVectorTable, sizeof(ExternalVectorTable), 0); + CpuSleep (); +} - // - // Get original IDT address and size. - // - AsmReadIdtr ((IA32_DESCRIPTOR *) &OldIdtPtr); +/** + Ensure the compatibility of a memory space descriptor with the MMIO aperture. + + The memory space descriptor can come from the GCD memory space map, or it can + represent a gap between two neighboring memory space descriptors. In the + latter case, the GcdMemoryType field is expected to be + EfiGcdMemoryTypeNonExistent. + + If the memory space descriptor already has type + EfiGcdMemoryTypeMemoryMappedIo, and its capabilities are a superset of the + required capabilities, then no action is taken -- it is by definition + compatible with the aperture. + + Otherwise, the intersection of the memory space descriptor is calculated with + the aperture. If the intersection is the empty set (no overlap), no action is + taken; the memory space descriptor is compatible with the aperture. + + Otherwise, the type of the descriptor is investigated again. If the type is + EfiGcdMemoryTypeNonExistent (representing a gap, or a genuine descriptor with + such a type), then an attempt is made to add the intersection as MMIO space + to the GCD memory space map, with the specified capabilities. This ensures + continuity for the aperture, and the descriptor is deemed compatible with the + aperture. + + Otherwise, the memory space descriptor is incompatible with the MMIO + aperture. + + @param[in] Base Base address of the aperture. + @param[in] Length Length of the aperture. + @param[in] Capabilities Capabilities required by the aperture. + @param[in] Descriptor The descriptor to ensure compatibility with the + aperture for. + + @retval EFI_SUCCESS The descriptor is compatible. The GCD memory + space map may have been updated, for + continuity within the aperture. + @retval EFI_INVALID_PARAMETER The descriptor is incompatible. + @return Error codes from gDS->AddMemorySpace(). +**/ +EFI_STATUS +IntersectMemoryDescriptor ( + IN UINT64 Base, + IN UINT64 Length, + IN UINT64 Capabilities, + IN CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor + ) +{ + UINT64 IntersectionBase; + UINT64 IntersectionEnd; + EFI_STATUS Status; - if ((OldIdtPtr.Base != 0) && ((OldIdtPtr.Limit & 7) == 7)) { - OldIdt = (IA32_IDT_GATE_DESCRIPTOR*) OldIdtPtr.Base; - OldIdtSize = (OldIdtPtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR); - // - // Save original IDT entry and IDT entry count. - // - mOrigIdtEntry = AllocateCopyPool (OldIdtPtr.Limit + 1, (VOID *) OldIdtPtr.Base); - ASSERT (mOrigIdtEntry != NULL); - mOrigIdtEntryCount = (UINT16) OldIdtSize; - } else { - OldIdt = NULL; - OldIdtSize = 0; + if (Descriptor->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo && + (Descriptor->Capabilities & Capabilities) == Capabilities) { + return EFI_SUCCESS; } - // - // Intialize IDT - // - CurrentCs = AsmReadCs(); - for (Index = 0; Index < INTERRUPT_VECTOR_NUMBER; Index ++) { + IntersectionBase = MAX (Base, Descriptor->BaseAddress); + IntersectionEnd = MIN (Base + Length, + Descriptor->BaseAddress + Descriptor->Length); + if (IntersectionBase >= IntersectionEnd) { // - // If the old IDT had a handler for this interrupt, then - // preserve it. + // The descriptor and the aperture don't overlap. // - if (Index < OldIdtSize) { - IntHandler = - (VOID*) ( - OldIdt[Index].Bits.OffsetLow + - (((UINTN) OldIdt[Index].Bits.OffsetHigh) << 16) -#if defined (MDE_CPU_X64) - + (((UINTN) OldIdt[Index].Bits.OffsetUpper) << 32) -#endif - ); - } else { - IntHandler = NULL; - } + return EFI_SUCCESS; + } + + if (Descriptor->GcdMemoryType == EfiGcdMemoryTypeNonExistent) { + Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo, + IntersectionBase, IntersectionEnd - IntersectionBase, + Capabilities); - gIdtTable[Index].Bits.Selector = CurrentCs; - gIdtTable[Index].Bits.Reserved_0 = 0; - gIdtTable[Index].Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32; - SetInterruptDescriptorTableHandlerAddress (Index, IntHandler); + DEBUG ((EFI_ERROR (Status) ? DEBUG_ERROR : DEBUG_VERBOSE, + "%a: %a: add [%Lx, %Lx): %r\n", gEfiCallerBaseName, __FUNCTION__, + IntersectionBase, IntersectionEnd, Status)); + return Status; } - // - // Load IDT Pointer - // - IdtPtrAlignmentBuffer = AllocatePool (sizeof (*IdtPtr) + 16); - IdtPtr = ALIGN_POINTER (IdtPtrAlignmentBuffer, 16); - IdtPtr->Base = (UINT32)(((UINTN)(VOID*) gIdtTable) & (BASE_4GB-1)); - IdtPtr->Limit = (UINT16) (sizeof (gIdtTable) - 1); + DEBUG ((DEBUG_ERROR, "%a: %a: desc [%Lx, %Lx) type %u cap %Lx conflicts " + "with aperture [%Lx, %Lx) cap %Lx\n", gEfiCallerBaseName, __FUNCTION__, + Descriptor->BaseAddress, Descriptor->BaseAddress + Descriptor->Length, + (UINT32)Descriptor->GcdMemoryType, Descriptor->Capabilities, + Base, Base + Length, Capabilities)); + return EFI_INVALID_PARAMETER; +} - AsmWriteIdtr (IdtPtr); +/** + Add MMIO space to GCD. + The routine checks the GCD database and only adds those which are + not added in the specified range to GCD. - FreePool (IdtPtrAlignmentBuffer); + @param Base Base address of the MMIO space. + @param Length Length of the MMIO space. + @param Capabilities Capabilities of the MMIO space. - // - // Initialize Exception Handlers - // - for (Index = OldIdtSize; Index < 32; Index++) { - Status = CpuRegisterInterruptHandler (&gCpu, Index, CommonExceptionHandler); - ASSERT_EFI_ERROR (Status); + @retval EFI_SUCCES The MMIO space was added successfully. +**/ +EFI_STATUS +AddMemoryMappedIoSpace ( + IN UINT64 Base, + IN UINT64 Length, + IN UINT64 Capabilities + ) +{ + EFI_STATUS Status; + UINTN Index; + UINTN NumberOfDescriptors; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap; + + Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: %a: GetMemorySpaceMap(): %r\n", + gEfiCallerBaseName, __FUNCTION__, Status)); + return Status; } - // - // Set the pointer to the array of C based exception handling routines. - // - InitializeExternalVectorTablePtr (ExternalVectorTable); + for (Index = 0; Index < NumberOfDescriptors; Index++) { + Status = IntersectMemoryDescriptor (Base, Length, Capabilities, + &MemorySpaceMap[Index]); + if (EFI_ERROR (Status)) { + goto FreeMemorySpaceMap; + } + } -} + DEBUG_CODE ( + // + // Make sure there are adjacent descriptors covering [Base, Base + Length). + // It is possible that they have not been merged; merging can be prevented + // by allocation and different capabilities. + // + UINT64 CheckBase; + EFI_STATUS CheckStatus; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor; + + for (CheckBase = Base; + CheckBase < Base + Length; + CheckBase = Descriptor.BaseAddress + Descriptor.Length) { + CheckStatus = gDS->GetMemorySpaceDescriptor (CheckBase, &Descriptor); + ASSERT_EFI_ERROR (CheckStatus); + ASSERT (Descriptor.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo); + ASSERT ((Descriptor.Capabilities & Capabilities) == Capabilities); + } + ); + +FreeMemorySpaceMap: + FreePool (MemorySpaceMap); + return Status; +} /** - Callback function for idle events. + Add and allocate CPU local APIC memory mapped space. - @param Event Event whose notification function is being invoked. - @param Context The pointer to the notification function's context, - which is implementation-dependent. + @param[in]ImageHandle Image handle this driver. **/ VOID -EFIAPI -IdleLoopEventCallback ( - IN EFI_EVENT Event, - IN VOID *Context +AddLocalApicMemorySpace ( + IN EFI_HANDLE ImageHandle ) { - CpuSleep (); -} + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS BaseAddress; + BaseAddress = (EFI_PHYSICAL_ADDRESS) GetLocalApicBaseAddress(); + Status = AddMemoryMappedIoSpace (BaseAddress, SIZE_4KB, EFI_MEMORY_UC); + ASSERT_EFI_ERROR (Status); + + // + // Try to allocate APIC memory mapped space, does not check return + // status because it may be allocated by other driver, or DXE Core if + // this range is built into Memory Allocation HOB. + // + Status = gDS->AllocateMemorySpace ( + EfiGcdAllocateAddress, + EfiGcdMemoryTypeMemoryMappedIo, + 0, + SIZE_4KB, + &BaseAddress, + ImageHandle, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a: %a: AllocateMemorySpace() Status - %r\n", + gEfiCallerBaseName, __FUNCTION__, Status)); + } +} /** Initialize the state information for the CPU Architectural Protocol. @@ -1246,6 +1164,8 @@ InitializeCpu ( { EFI_STATUS Status; EFI_EVENT IdleLoopEvent; + + InitializePageTableLib(); InitializeFloatingPointUnits (); @@ -1264,11 +1184,6 @@ InitializeCpu ( // InitInterruptDescriptorTable (); - // - // Enable the local APIC for Virtual Wire Mode. - // - ProgramVirtualWireMode (); - // // Install CPU Architectural Protocol // @@ -1284,6 +1199,11 @@ InitializeCpu ( // RefreshGcdMemoryAttributes (); + // + // Add and allocate local APIC memory mapped space + // + AddLocalApicMemorySpace (ImageHandle); + // // Setup a callback for idle events // @@ -1297,6 +1217,8 @@ InitializeCpu ( ); ASSERT_EFI_ERROR (Status); + InitializeMpSupport (); + return Status; }