X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FCpuDxe%2FCpuDxe.c;h=6ae2dcd1c71136bd6fa364ba906dd80a6befcde6;hp=35fba39b43aeb192cbf3f0ac9dffbaf00d946f01;hb=147fd35c3e389ecd025dbfd243312bf5b22da7c9;hpb=c52acd89e872e32eae7ae6265b86b1670cd9cac1 diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c index 35fba39b43..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 - 2010, 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,73 +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; -UINT8 mDefaultMemoryType = MTRR_CACHE_WRITE_BACK; -UINT64 mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS; -UINT64 mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK; +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 }, @@ -99,251 +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 -// -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. @@ -501,21 +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; - } - - SetInterruptDescriptorTableHandlerAddress ((UINTN)InterruptType, NULL); - ExternalVectorTable[InterruptType] = InterruptHandler; - return EFI_SUCCESS; + return RegisterCpuInterruptHandler (InterruptType, InterruptHandler); } @@ -551,6 +299,9 @@ CpuGetTimerValue ( OUT UINT64 *TimerPeriod OPTIONAL ) { + UINT64 BeginValue; + UINT64 EndValue; + if (TimerValue == NULL) { return EFI_INVALID_PARAMETER; } @@ -562,30 +313,70 @@ CpuGetTimerValue ( *TimerValue = AsmReadTsc (); if (TimerPeriod != NULL) { + if (mTimerPeriod == 0) { + // + // Read time stamp counter before and after delay of 100 microseconds // - // BugBug: Hard coded. Don't know how to do this generically + BeginValue = AsmReadTsc (); + MicroSecondDelay (100); + EndValue = AsmReadTsc (); // - *TimerPeriod = 1000000000; + // Calculate the actual frequency + // + mTimerPeriod = DivU64x64Remainder ( + MultU64x32 ( + 1000 * 1000 * 1000, + 100 + ), + EndValue - BeginValue, + NULL + ); + } + *TimerPeriod = mTimerPeriod; } return EFI_SUCCESS; } - /** - Set memory cacheability attributes for given range of memeory. + A minimal wrapper function that allows MtrrSetAllMtrrs() to be passed to + EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() as Procedure. - @param This Protocol instance structure - @param BaseAddress Specifies the start address of the - memory range - @param Length Specifies the length of the memory range - @param Attributes The memory cacheability for the memory range + @param[in] Buffer Pointer to an MTRR_SETTINGS object, to be passed to + MtrrSetAllMtrrs(). +**/ +VOID +EFIAPI +SetMtrrsFromBuffer ( + IN VOID *Buffer + ) +{ + MtrrSetAllMtrrs (Buffer); +} - @retval EFI_SUCCESS If the cacheability of that memory range is - set successfully - @retval EFI_UNSUPPORTED If the desired operation cannot be done - @retval EFI_INVALID_PARAMETER The input parameter is not correct, - such as Length = 0 +/** + Implementation of SetMemoryAttributes() service of CPU Architecture Protocol. + + This function modifies the attributes for the memory region specified by BaseAddress and + Length from their current attributes to the attributes specified by Attributes. + + @param This The EFI_CPU_ARCH_PROTOCOL instance. + @param BaseAddress The physical address that is the start address of a memory region. + @param Length The size in bytes of the memory region. + @param Attributes The bit mask of attributes to set for the memory region. + + @retval EFI_SUCCESS The attributes were set for the memory region. + @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by + BaseAddress and Length cannot be modified. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes specified an illegal combination of attributes that + cannot be set together. + @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of + the memory resource range. + @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory + resource range specified by BaseAddress and Length. + The bit mask of attributes is not support for the memory resource + range specified by BaseAddress and Length. **/ EFI_STATUS @@ -599,12 +390,12 @@ CpuSetMemoryAttributes ( { RETURN_STATUS Status; MTRR_MEMORY_CACHE_TYPE CacheType; - - if (!IsMtrrSupported ()) { - return EFI_UNSUPPORTED; - } - - DEBUG((EFI_D_ERROR, "CpuAp: SetMemorySpaceAttributes(BA=%08x, Len=%08x, Attr=%08x)\n", BaseAddress, Length, Attributes)); + 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 @@ -613,47 +404,105 @@ CpuSetMemoryAttributes ( // to avoid unnecessary computing. // if (mIsFlushingGCD) { - DEBUG((EFI_D_ERROR, " Flushing GCD\n")); - return EFI_SUCCESS; + DEBUG((DEBUG_INFO, " Flushing GCD\n")); + return EFI_SUCCESS; + } + + // + // 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; + } + + CacheAttributes = Attributes & CACHE_ATTRIBUTE_MASK; + MemoryAttributes = Attributes & MEMORY_ATTRIBUTE_MASK; + + if (Attributes != (CacheAttributes | MemoryAttributes)) { + return EFI_INVALID_PARAMETER; + } + + if (CacheAttributes != 0) { + if (!IsMtrrSupported ()) { + return EFI_UNSUPPORTED; } - switch (Attributes) { - case EFI_MEMORY_UC: - CacheType = CacheUncacheable; - break; + switch (CacheAttributes) { + case EFI_MEMORY_UC: + CacheType = CacheUncacheable; + break; - case EFI_MEMORY_WC: - CacheType = CacheWriteCombining; - break; + case EFI_MEMORY_WC: + CacheType = CacheWriteCombining; + break; - case EFI_MEMORY_WT: - CacheType = CacheWriteThrough; - break; + case EFI_MEMORY_WT: + CacheType = CacheWriteThrough; + break; - case EFI_MEMORY_WP: - CacheType = CacheWriteProtected; - break; + case EFI_MEMORY_WP: + CacheType = CacheWriteProtected; + break; - case EFI_MEMORY_WB: - CacheType = CacheWriteBack; - break; + case EFI_MEMORY_WB: + CacheType = CacheWriteBack; + break; - default: - return EFI_UNSUPPORTED; + 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 // - DEBUG((EFI_D_ERROR, " MtrrSetMemoryAttribute()\n")); - Status = MtrrSetMemoryAttribute( - BaseAddress, - Length, - CacheType - ); - - MtrrDebugPrintAllMtrrs (); - - return (EFI_STATUS) Status; + return AssignMemoryPageAttributes (NULL, BaseAddress, Length, MemoryAttributes, NULL); } /** @@ -676,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; } /** @@ -850,7 +698,7 @@ SetGcdMemorySpaceAttributes ( **/ VOID -RefreshGcdMemoryAttributes ( +RefreshMemoryAttributesFromMtrr ( VOID ) { @@ -869,16 +717,11 @@ RefreshGcdMemoryAttributes ( VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR]; MTRR_FIXED_SETTINGS MtrrFixedSettings; UINT32 FirmwareVariableMtrrCount; - - if (!IsMtrrSupported ()) { - return; - } + UINT8 DefaultMemoryType; FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount (); ASSERT (FirmwareVariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR); -// mIsFlushingGCD = TRUE; - mIsFlushingGCD = FALSE; MemorySpaceMap = NULL; // @@ -904,7 +747,8 @@ RefreshGcdMemoryAttributes ( ); ASSERT_EFI_ERROR (Status); - DefaultAttributes = GetMemorySpaceAttributeFromMtrrType (mDefaultMemoryType); + DefaultMemoryType = (UINT8) MtrrGetDefaultMemoryType (); + DefaultAttributes = GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType); // // Set default attributes to all spaces. @@ -936,12 +780,14 @@ RefreshGcdMemoryAttributes ( ); } } + // - // Go for variable MTRRs with Non-WB attribute + // Go for variable MTRRs with the attribute except for WB and UC attributes // for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) { if (VariableMtrr[Index].Valid && - VariableMtrr[Index].Type != MTRR_CACHE_WRITE_BACK) { + VariableMtrr[Index].Type != MTRR_CACHE_WRITE_BACK && + VariableMtrr[Index].Type != MTRR_CACHE_UNCACHEABLE) { Attributes = GetMemorySpaceAttributeFromMtrrType ((UINT8) VariableMtrr[Index].Type); SetGcdMemorySpaceAttributes ( MemorySpaceMap, @@ -953,6 +799,22 @@ RefreshGcdMemoryAttributes ( } } + // + // Go for variable MTRRs with UC attribute + // + for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) { + if (VariableMtrr[Index].Valid && + VariableMtrr[Index].Type == MTRR_CACHE_UNCACHEABLE) { + SetGcdMemorySpaceAttributes ( + MemorySpaceMap, + NumberOfDescriptors, + VariableMtrr[Index].BaseAddress, + VariableMtrr[Index].Length, + EFI_MEMORY_UC + ); + } + } + // // Go for fixed MTRRs // @@ -1010,35 +872,49 @@ 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)); } +/** + Refreshes the GCD Memory Space attributes according to MTRRs and Paging. + + 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; +} /** Initialize Interrupt Descriptor Table for interrupt handling. @@ -1049,86 +925,224 @@ InitInterruptDescriptorTable ( VOID ) { - 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); + 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); +} - // - // Get original IDT address and size. - // - AsmReadIdtr ((IA32_DESCRIPTOR *) &OldIdtPtr); - if ((OldIdtPtr.Base != 0) && ((OldIdtPtr.Limit & 7) == 7)) { - OldIdt = (IA32_IDT_GATE_DESCRIPTOR*) OldIdtPtr.Base; - OldIdtSize = (OldIdtPtr.Limit + 1) / 8; - } else { - OldIdt = NULL; - OldIdtSize = 0; +/** + 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 +EFIAPI +IdleLoopEventCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + CpuSleep (); +} + +/** + 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 (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 + - (OldIdt[Index].Bits.OffsetHigh << 16) -#if defined (MDE_CPU_X64) - + (((UINTN) OldIdt[Index].Bits.OffsetUpper) << 32) -#endif - ); - } else { - IntHandler = NULL; - } + return EFI_SUCCESS; + } - gIdtTable[Index].Bits.Selector = CurrentCs; - gIdtTable[Index].Bits.Reserved_0 = 0; - gIdtTable[Index].Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32; - SetInterruptDescriptorTableHandlerAddress (Index, IntHandler); + if (Descriptor->GcdMemoryType == EfiGcdMemoryTypeNonExistent) { + Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo, + IntersectionBase, IntersectionEnd - IntersectionBase, + Capabilities); + + 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 = 0; 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; } +/** + Add and allocate CPU local APIC memory mapped space. + + @param[in]ImageHandle Image handle this driver. + +**/ +VOID +AddLocalApicMemorySpace ( + IN EFI_HANDLE ImageHandle + ) +{ + 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. @@ -1149,6 +1163,11 @@ InitializeCpu ( ) { EFI_STATUS Status; + EFI_EVENT IdleLoopEvent; + + InitializePageTableLib(); + + InitializeFloatingPointUnits (); // // Make sure interrupts are disabled @@ -1180,6 +1199,26 @@ InitializeCpu ( // RefreshGcdMemoryAttributes (); + // + // Add and allocate local APIC memory mapped space + // + AddLocalApicMemorySpace (ImageHandle); + + // + // Setup a callback for idle events + // + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + IdleLoopEventCallback, + NULL, + &gIdleLoopEventGuid, + &IdleLoopEvent + ); + ASSERT_EFI_ERROR (Status); + + InitializeMpSupport (); + return Status; }