接上文ARM系列之ARM多核指令WFE、WFI、SEV原理
本文主要对WFI使用注意事项补充说明。
Hint instruction is for the instruction set space that is reserved for
architectural hint instructions.Some encodings described here are not allocated in this revision of the architecture,
and behave as NOPs. These encodings might be allocated to other hint functionality
in future revisions of the architecture and therefore must not be used by software.
HINT 指令可以合法地被视为 NOP指令,但它们可以具有特定于实现的效果,常见的HINT指令有:
NOP // No operation,无操作, 不保证CPU会花时间去执行
YIELD // 提示当前线程正在执行可以换出的任务
WFE // Wait for Event,进入low power状态,直到等待的事件发生
WFI // Wait for interrupt,进入low power状态,直到等待的中断或与中断类似的操作发生
SEV // Send Event,发送事件,与WFE对应
SEVL // Send Event Local,发送本地事件,与WFE对应
ARM 汇编语言中包含可用于让core进入低功耗状态(low-power state)的指令:WFI或WFE。ARM架构将这些指令定义为hint指令,这意味着core在执行它们时不需要采取任何特定操作。然而,在 Cortex-A 处理器系列中,这些指令的实现方式是关闭几乎所有内核部分的时钟。这意味着内核的功耗显着降低,仅消耗静态漏电流,没有动态功耗。
SystemHintOp op;case CRm:op2 ofwhen '0000 000' op = SystemHintOp_NOP;when '0000 001' op = SystemHintOp_YIELD;when '0000 010' op = SystemHintOp_WFE;when '0000 011' op = SystemHintOp_WFI;when '0000 100' op = SystemHintOp_SEV;when '0000 101' op = SystemHintOp_SEVL;when '0000 110'if !HaveDGHExt() then EndOfInstruction(); // Instruction executes as NOPop = SystemHintOp_DGH;when '0000 111' SEE "XPACLRI";when '0001 xxx'case op2 ofwhen '000' SEE "PACIA1716";when '010' SEE "PACIB1716";when '100' SEE "AUTIA1716";when '110' SEE "AUTIB1716";otherwise EndOfInstruction();when '0010 000'if !HaveRASExt() then EndOfInstruction(); // Instruction executes as NOPop = SystemHintOp_ESB;when '0010 001'if !HaveStatisticalProfiling() then EndOfInstruction(); // Instruction executes as NOPop = SystemHintOp_PSB;when '0010 010'if !HaveSelfHostedTrace() then EndOfInstruction(); // Instruction executes as NOPop = SystemHintOp_TSB;when '0010 100'op = SystemHintOp_CSDB;when '0011 xxx'case op2 ofwhen '000' SEE "PACIAZ";when '001' SEE "PACIASP";when '010' SEE "PACIBZ";when '011' SEE "PACIBSP";when '100' SEE "AUTIAZ";when '101' SEE "AUTHASP";when '110' SEE "AUTIBZ";when '111' SEE "AUTIBSP";when '0100 xx0'op = SystemHintOp_BTI;// Check branch target compatibility between BTI instruction and PSTATE.BTYPESetBTypeCompatible(BTypeCompatible_BTI(op2<2:1>));otherwise EndOfInstruction();
case op ofwhen SystemHintOp_YIELDHint_Yield();when SystemHintOp_DGHHint_DGH();when SystemHintOp_WFEHint_WFE(-1, WFxType_WFE);when SystemHintOp_WFIHint_WFI(-1, WFxType_WFI);when SystemHintOp_SEVSendEvent();when SystemHintOp_SEVLSendEventLocal();when SystemHintOp_ESBif HaveTME() && TSTATE.depth > 0 thenFailTransaction(TMFailure_ERR, FALSE);SynchronizeErrors();AArch64.ESBOperation();if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then AArch64.vESBOperation();TakeUnmaskedSErrorInterrupts();when SystemHintOp_PSBProfilingSynchronizationBarrier();when SystemHintOp_TSBTraceSynchronizationBarrier();when SystemHintOp_CSDBConsumptionOfSpeculativeDataBarrier();when SystemHintOp_BTISetBTypeNext('00');otherwise // do nothing
WFI指令的主要目的就是使core进入standby模式,直到中断或者类似中断的事件发送,才退出,core继续工作。
在待机模式下,core保持上电状态,但其大部分时钟停止或者进入时钟门限。这意味着core的绝大部分都处于static state,唯一消耗的功率是用于寻找中断唤醒条件的泄漏电流和少量逻辑时钟。
使用 WFI(等待中断)或 WFE(等待事件)指令可以进入此模式。 ARM 建议在 WFI 或 WFE 之前使用数据同步屏障 (Data Synchronization Barrier ,DSB) 指令,以确保待处理的内存事务在更改状态之前完成。
core进入待机状态后,会停止执行,直到检测到唤醒事件。唤醒条件取决于进入指令。对于 WFI,需要中断事件或外部调试请求来唤醒core。对于 WFE,存在许多指定的事件,包括cluster中执行 SEV 指令的另一个core。
WFI 指令广泛用于电池供电的系统。例如,手机可以每秒多次将core置于待机模式,同时等待用户按下按钮才唤醒core,从而可以节省功耗。
WFE 类似于 WFI。core暂停执行,直到发生事件。这可以是列出的事件条件之一,也可以是cluster中另一个core发出的事件信号。其他core可以通过执行 SEV 指令来发出事件信号。 SEV 向所有core发送一个事件信号。还可以对generic timer通用定时器进行编程,以触发将core从 WFE 唤醒的周期性事件。
总而言之,WFI 指令可以提示hint core在接收到中断或类似的exception之前无需执行任何操作,具体来说有两部分:
强制暂停core的运行以及所有相关的总线活动。
暂停处理器执行指令。
/* 案例一*/
/* 假设这是个timer,10秒后将发起中断,* 中断handler在其他地方定义 */
Timer(10);
/* 如果处理器没有接收到中断,WFI指令将没有完成,* 一直处于休眠状态,下一条printf也不会被执行。* 当检测到中断时(10S后),WFI指令才算完成,* 才会唤醒处理器,下一条printf会被执行* */
WFI();
printf("中断发生,WFI检测到中断");
/* 案例二 */
/* 10秒后将发起中断 */
Timer(10);
/* 执行这个函数需要20秒 */
WasteTime(20);
/* 从中断发生,到处理完成中断用不了10秒,执行到WFI时,中断已经消失了,
* 所以CPU会一直处于休眠状态,下面的printf也不会被执行
* */
WFI();
printf("中断发生,WFI检测到中断");
上一篇:29日NBA前瞻:字母哥锡安再次上演怪兽对决 联盟第一力争复仇老鹰 29日NBA前瞻:字母哥锡安再次上演怪兽对决 联盟第一力争复仇老鹰
下一篇:成本超1亿,票房仅25.6万,新片清明档垫底,收手吧烂片王包贝尔 成本超1亿,票房仅25.6万,新片清明档垫底,收手吧烂片王包贝尔