前面的实验已经证明:UEFI 架构本身只有一个中断,那就是时间中断,在实体机上测试显示使用的是 HPET 的时间中断。那么我们是否可以再给UEFI多加一个中断?答案是肯定的,只要硬件上有支持,我们就可以多加入一个中断。这次我们实验的就是 8254 上面的中断。8254 根据我的理解,就是一个能够发出脉冲的时间芯片。具体脉冲发出来了,还需要有一个中断芯片来管理这些中断,在 Leagcy 的X86中,这个工作是 8259 来完成的,下面就是各种微机原理都会展示的图片。因为中断比较多,所以需要2片来级联进行扩展。对于我们的想法来说,只要知道8254 是接在第一个(主,Master) 8259的Pin0 上(IRQ0)即可。
一直困扰我的问题是 IRQx对应在IDT 中的哪一个,直到最近看到了 EFI_LEGACY_8259_PROTOCOL 中的 GetVector 函数【参考1】:
这个函数的作用就是返回当前IRQ 的Vector,也是就是IDT中的入口号。
了解了上面一些,就可以动手写程序了。使用我们之前的 PrintDriver3 的架构,使用 8259 的 Protocol来完成 8254 的初始化,设定 Interrupt 的Handler 是 MyInterruptHander,在这个函数中对 Var2 自增,最后使用之前的 PDT3 Application 打印 Var2 的数值。运行起来我们的Driver 之后,8254会不断发送中断出来,直观的说就是代码会运行到MyInterruptHander中,我们在这个函数中对于做标记的变量自增,最后用另外的 Protocol 找到这个变量并且打印出来。
完整代码如下:
/** @file This driver produces Print9 protocol layered on top of the PrintLib from the MdePkg. Copyright (c) 2009, Intel Corporation. All rights reserved.<BR> 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 http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ #include <PiDxe.h> #include <Library/UefiLib.h> #include <Library/PrintLib.h> #include <Library/UefiBootServicesTableLib.h> #include <Library/DebugLib.h> #include <Library/UefiDriverEntryPoint.h> #include <Library/MemoryAllocationLib.h> #include "Print9.h" #include <Library/IoLib.h> #include <Protocol/Cpu.h> #include <Library/CpuLib.h> #include "Legacy8259.h" extern EFI_SYSTEM_TABLE *gST; EFI_GUID gEfiPrint9ProtocolGuid = { 0xf05976ef, 0x83f1, 0x4f3d, { 0x86, 0x19, 0xf7, 0x59,0x5d, 0x41, 0xe5, 0x61 } }; EFI_GUID gEfiCpuArchProtocolGuid = { 0x26BACCB1, 0x6F42, 0x11D4, { 0xBC, 0xE7, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81 }}; // Include/Protocol/Legacy8259.h EFI_GUID gEfiLegacy8259ProtocolGuid = { 0x38321dba, 0x4fe0, 0x4e17, { 0x8a, 0xec, 0x41, 0x30, 0x55, 0xea, 0xed, 0xc1 }}; //\PcAtChipsetPkg\8254TimerDxe\Timer.h // // The maximum tick duration for 8254 timer // #define MAX_TIMER_TICK_DURATION 549254 // // The default timer tick duration is set to 10 ms = 100000 100 ns units // #define DEFAULT_TIMER_TICK_DURATION 100000 #define TIMER_CONTROL_PORT 0x43 #define TIMER0_COUNT_PORT 0x40 // // The current period of the timer interrupt // volatile UINT64 mTimerPeriod = 0; EFI_PRINT9_PRIVATE_DATA *Image; EFI_HANDLE mPrintThunkHandle = NULL; // // Pointer to the Legacy 8259 Protocol instance // EFI_LEGACY_8259_PROTOCOL *mLegacy8259; //Copied from \MdeModulePkg\Library\DxePrintLibPrint2Protocol\PrintLib.c UINTN EFIAPI MyPrint () { CHAR16 *Buffer=L"1 2 3 4 5 6 7 8 9 0 A B C E D F "; UnicodeSPrint(Buffer,16,L"%d\r\n",Image->Var2); gST->ConOut->OutputString(gST->ConOut,Buffer); return 0; } // // Worker Functions // /** Sets the counter value for Timer #0 in a legacy 8254 timer. @param Count The 16-bit counter value to program into Timer #0 of the legacy 8254 timer. **/ VOID SetPitCount ( IN UINT16 Count ) { IoWrite8 (TIMER_CONTROL_PORT, 0x36); IoWrite8 (TIMER0_COUNT_PORT, (UINT8)(Count & 0xff)); IoWrite8 (TIMER0_COUNT_PORT, (UINT8)((Count >> 8) & 0xff)); } /** This function adjusts the period of timer interrupts to the value specified by TimerPeriod. If the timer period is updated, then the selected timer period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If the timer hardware is not programmable, then EFI_UNSUPPORTED is returned. If an error occurs while attempting to update the timer period, then the timer hardware will be put back in its state prior to this call, and EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt is disabled. This is not the same as disabling the CPU's interrupts. Instead, it must either turn off the timer hardware, or it must adjust the interrupt controller so that a CPU interrupt is not generated when the timer interrupt fires. @param This The EFI_TIMER_ARCH_PROTOCOL instance. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If the timer hardware is not programmable, then EFI_UNSUPPORTED is returned. If the timer is programmable, then the timer period will be rounded up to the nearest timer period that is supported by the timer hardware. If TimerPeriod is set to 0, then the timer interrupts will be disabled. @retval EFI_SUCCESS The timer period was changed. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error. **/ EFI_STATUS EFIAPI TimerDriverSetTimerPeriod ( IN UINT64 TimerPeriod ) { UINT64 TimerCount; // // The basic clock is 1.19318 MHz or 0.119318 ticks per 100 ns. // TimerPeriod * 0.119318 = 8254 timer divisor. Using integer arithmetic // TimerCount = (TimerPeriod * 119318)/1000000. // // Round up to next highest integer. This guarantees that the timer is // equal to or slightly longer than the requested time. // TimerCount = ((TimerPeriod * 119318) + 500000)/1000000 // // Note that a TimerCount of 0 is equivalent to a count of 65,536 // // Since TimerCount is limited to 16 bits for IA32, TimerPeriod is limited // to 20 bits. // if (TimerPeriod == 0) { // // Disable timer interrupt for a TimerPeriod of 0 // mLegacy8259->DisableIrq (mLegacy8259, Efi8259Irq0); } else { // // Convert TimerPeriod into 8254 counts // TimerCount = DivU64x32 (MultU64x32 (119318, (UINT32) TimerPeriod) + 500000, 1000000); // // Check for overflow // if (TimerCount >= 65536) { TimerCount = 0; TimerPeriod = MAX_TIMER_TICK_DURATION; } // // Program the 8254 timer with the new count value // SetPitCount ((UINT16) TimerCount); // // Enable timer interrupt // mLegacy8259->EnableIrq (mLegacy8259, Efi8259Irq0, FALSE); } // // Save the new timer period // mTimerPeriod = TimerPeriod; return EFI_SUCCESS; } VOID EFIAPI MyInterruptHandler ( IN EFI_EXCEPTION_TYPE InterruptType, IN EFI_SYSTEM_CONTEXT SystemContext ) { EFI_TPL OriginalTPL; OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL); mLegacy8259->EndOfInterrupt (mLegacy8259, Efi8259Irq0); Image->Var2++; gBS->RestoreTPL (OriginalTPL); } /** The user Entry Point for Print module. This is the entry point for Print DXE Driver. It installs the Print2 Protocol. @param[in] ImageHandle The firmware allocated handle for the EFI image. @param[in] SystemTable A pointer to the EFI System Table. @retval EFI_SUCCESS The entry point is executed successfully. @retval Others Some error occurs when executing this entry point. **/ EFI_STATUS EFIAPI PrintEntryPoint ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { EFI_STATUS Status; EFI_CPU_ARCH_PROTOCOL *mCpu; UINT32 TimerVector; // // Allocate a new image structure // Image = AllocateZeroPool (sizeof(EFI_PRINT9_PRIVATE_DATA)); if (Image == NULL) { Status = EFI_OUT_OF_RESOURCES; ASSERT_EFI_ERROR (Status); } Image->Signature = PRINT9_PRIVATE_DATA_SIGNATURE; Image->PRINT9.UnicodeBSPrint=UnicodeBSPrint; Image->PRINT9.UnicodeSPrint=UnicodeSPrint; Image->PRINT9.UnicodeBSPrintAsciiFormat=UnicodeBSPrintAsciiFormat; Image->PRINT9.UnicodeSPrintAsciiFormat=MyPrint; //Image->PRINT9.UnicodeValueToString=UnicodeValueToString; Image->PRINT9.AsciiBSPrint=AsciiBSPrint; Image->PRINT9.AsciiSPrint=AsciiSPrint; Image->PRINT9.AsciiBSPrintUnicodeFormat=AsciiBSPrintUnicodeFormat; Image->PRINT9.AsciiSPrintUnicodeFormat=AsciiSPrintUnicodeFormat; //Image->PRINT9.AsciiValueToString=AsciiValueToString; Image->Var2=1984; Status = gBS->InstallMultipleProtocolInterfaces ( &mPrintThunkHandle, &gEfiPrint9ProtocolGuid, &Image->PRINT9, NULL ); ASSERT_EFI_ERROR (Status); // // Locate the Cpu Arch Protocol. // Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, &mCpu); ASSERT_EFI_ERROR (Status); // // Find the Legacy8259 protocol. // Status = gBS->LocateProtocol (&gEfiLegacy8259ProtocolGuid, NULL, (VOID **) &mLegacy8259); ASSERT_EFI_ERROR (Status); // // Force the timer to be disabled // Status = TimerDriverSetTimerPeriod (0); ASSERT_EFI_ERROR (Status); // // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver // TimerVector = 0; Status = mLegacy8259->GetVector (mLegacy8259, Efi8259Irq0, (UINT8 *) &TimerVector); ASSERT_EFI_ERROR (Status); // // Install interrupt handler for 8254 Timer #0 (ISA IRQ0) // Status = mCpu->RegisterInterruptHandler (mCpu, TimerVector, MyInterruptHandler); ASSERT_EFI_ERROR (Status); // // Force the timer to be enabled at its default period // Status = TimerDriverSetTimerPeriod (DEFAULT_TIMER_TICK_DURATION); ASSERT_EFI_ERROR (Status); return Status; }
运行结果(实体机):
从运行结果来看,代码中设定的定时器能够正常工作和我们的预期一致。
8254初始化部分代码拷贝自 8254TimerDxe\Timer.c ,很多具体操作的含义并不清楚,有兴趣的朋友可以找一本《微机原理》的书籍对照研究一下。X86架构依然兼容很久之前的 8254、8259的设计。这样的兼容性既是X86的优点,保证了过去的代码依然能够运行;同时也是X86沉重的历史包袱,而后面这一点更加是BIOS存在的意义。
EFI (X64)下载
printdriver4
源代码
PrintDriver4SRC
参考:
1. Intel® Platform Innovation Framework for UEFI Compatibility Support Module Specification