<?xml version="1.0" encoding="gbk"?> <rss version="2.0"><channel> <title>定阅帖子更新</title> <link>http://www.broadkey.com.cn/XML.ASP</link><description>TEAM Board - 意得法电子</description> <copyright>TEAM 2.0.5 Release</copyright><generator>TEAM Board by TEAM5.Cn Studio</generator> <ttl>30</ttl><item><link>http://www.broadkey.com.cn/Thread.asp?tid=55 </link><title>PowerPC&amp;nbsp;体系结构之中断异常</title><author>alan</author><pubDate>2009-11-25 23:45:18</pubDate><description><![CDATA[取 BOOKE 之精要。<br />
<br />
<strong>1. 异常类型</strong><br />
<br />
<font color="#ff0000">00</font>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Critical Interrupt&nbsp;&nbsp;&nbsp;&nbsp; 来自于外部中断控制器，具有较高的优先级<br />
<font color="#ff0000">01</font>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Machine Check&nbsp;&nbsp;&nbsp; 严重的内部状态错误，如 Cache 数据的校验失败<br />
02&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Data Storage&nbsp;&nbsp;&nbsp; 数据读写异常，如：用户态读一个非用户态的页 (UR=0)<br />
03&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Instruction Storage&nbsp;&nbsp;&nbsp; 读指令异常，如：用户态时取一个用户态不可执行的页 (UX=0)<br />
<font color="#0baa20">04</font>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; External Interrupt&nbsp;&nbsp;&nbsp; 来自于外部中断控制器<br />
05&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Alignment&nbsp;&nbsp;&nbsp; 非对齐访问异常<br />
<font color="#0baa20">06</font>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Program&nbsp;&nbsp;&nbsp; 程序异常，如：执行非法指令，用户态执行特权指令<br />
07&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Floating-Point unavailable &nbsp;&nbsp; 在无浮点部件的 CPU 上执行浮点指令即会触发此异常<br />
<font color="#0baa20">08</font>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System call&nbsp;&nbsp;&nbsp; 系统调用<br />
09&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Auxiliary Processor Unavailable &nbsp;&nbsp; 在无协处理器的 CPU 上执行协处理器指令即触发此异常&nbsp; <br />
10 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Decrementer&nbsp;&nbsp;&nbsp; DEC 寄存器归零异常，DEC 是一个内部时钟计数器，Linux 用之实现时钟中断<br />
11 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fixed-interval timer interrupt&nbsp;&nbsp;&nbsp; <br />
<font color="#ff0000">12</font> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Watchdog timer interrupt&nbsp;&nbsp;&nbsp; <br />
<font color="#0baa20">13</font> &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Data TLB error&nbsp;&nbsp;&nbsp; 数据 TLB Miss 异常<br />
<font color="#0baa20">14</font> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Instruction TLB error&nbsp;&nbsp;&nbsp; 指令 TLB Miss 异常<br />
<font color="#ff0000">15</font> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Debug&nbsp;&nbsp;&nbsp; 调试异常，用于支持调试<br />
<br />
16 - 31&nbsp;&nbsp; Reserved for future use &nbsp; 保留给将来体系结构升级用<br />
32 - 63&nbsp;&nbsp; Allocated for implementation-dependent use &nbsp; 具体实现相关<br />
<br />
其中 0，1，12，15 为 Critical Exception，当其发生时，使用 CSRR0 &amp; CSRR1 保存当前 PC 或 (PC + 4) 和 MSR；其他异常发生时，则使用 SRR0 &amp; SRR1 保存当前 PC 或 (PC + 4) 和 MSR<br />
<br />
可以看到 BOOKE 体系结构层面规定的异常即为前 16 个，这其中的有些异常是个笼统的抽象（比如 Data Storage 就需要区分是读还是写导致的），为了更细地描述发生异常的原因，PowerPC 引入了一个 ESR (Exception Syndrome Register)，让硬件在异常发生时，在其中指出更具体的原因。比如 若 ESR[40] 被置位，则说明异常是由写操作引起的。<br />
<br />
<br />
<strong>2. 异常入口（向量）<br />
</strong><br />
BOOKE 使用可读写的内部寄存器 IVPR 和 IVOR 来指定异常的入口。<br />
<br />
其中 IVPR (Interrupt Vector Prefix Register) 为 64 bit，指定所有异常入口基地址的高 48 bit，即 IVPR[48:63] 始终为 0<br />
<br />
IVOR (Interrupt Vector Offset Register) 为 32 bit，指定具体异常入口相对异常基地址的偏移，每个异常一个，只使用其低 16 位。则其 IVOR[32:40] 为 0；又因所有入口 16 字节对齐，实际上 IVOR[60:63] 亦始终 0：<br />
<br />
IVOR00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Critical Interrupt&nbsp;&nbsp;&nbsp;&nbsp; <br />
IVOR01&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Machine Check&nbsp;&nbsp;&nbsp; <br />
IVOR02&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Data Storage&nbsp;&nbsp; <br />
IVOR03&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Instruction Storage&nbsp;&nbsp;&nbsp; <br />
IVOR04&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; External Interrupt&nbsp;&nbsp;&nbsp; <br />
IVOR05&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Alignment&nbsp;&nbsp;&nbsp; <br />
IVOR06&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Program&nbsp;&nbsp;&nbsp; <br />
IVOR07&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Floating-Point unavailable&nbsp;&nbsp;&nbsp; <br />
IVOR08&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System call&nbsp;&nbsp;&nbsp; <br />
IVOR09&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Auxiliary Processor Unavailable&nbsp;&nbsp;&nbsp; <br />
IVOR10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Decrementer&nbsp;&nbsp;&nbsp; <br />
IVOR11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fixed-interval timer interrupt&nbsp;&nbsp; <br />
IVOR12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Watchdog timer interrupt&nbsp;&nbsp; <br />
<strong>IVOR13&nbsp;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Data TLB error&nbsp;&nbsp;&nbsp; <br />
IVOR14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Instruction TLB error&nbsp;&nbsp;&nbsp; <br />
IVOR15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug&nbsp;&nbsp;&nbsp; <br />
IVOR16 ~ IVOR31<br />
IVOR32 ~ IVOR63<br />
<br />
则 Data TLB Miss 的异常入口即为： IVPR[0:47] || IVOR[48:59] || 0b0000<br />
<br />
<strong>注意：</strong>PowerPC 没有 RESET 异常这个概念，故没有 RESET 的入口，上电后处理器直接到固定地址去取指令(E500 上为 0xFFFF FFFC)。<br />
<br />
<br />
<strong>3. 相关寄存器</strong><br />
<br />
<strong>3.0 MSR </strong>(Machine Status Register)<br />
<br />
内含部分异常使能位，清位则屏蔽相应的异常：<br />
<br />
MSR[46], CE (Critical Enable) --- Critical Input and Watchdog Timer Interrupts Enable<br />
MSR[48], EE (External Enable) --- External Input, Decrementer and Fixed-Interval Timer Interrupts Enable<br />
MSR[51], ME (Machine Check Enable) --- Machine Check Enable<br />
MSR[54], DE (Debug Enable) --- Debug Enable<br />
<br />
MSR[49], PR (Proble State) --- 置 1 表示处理器处于用户态，置 0 则为核心态<br />
<br />
当 Critical Exception 发生时，硬件将 MSR 保存于 CSRR1 后，仅保持 ME 不变，自动将 MSR 之其它位清零（PR = 0，进入核心态）<br />
<br />
当一般异常发生时，硬件将 MSR 保存于 SRR1 后，保持 CE，DE，ME 不变，自动将其它位清零<br />
<br />
<strong><br />
3.1 SRR0</strong> (Save/Restore Register 0)<br />
<br />
64 bit，用于异常发生时保存引起异常指令的地址（异常）或其下一条指令的地址（中断）。<br />
保存过程硬件自动做<br />
执行 rfi 从异常返回时，处理器会跳转到 SRR0 保存的地址处继续执行。<br />
<br />
<br />
<strong>3.2 SRR1 </strong>(Save/Restore Register 1)<br />
<br />
32 bit，用于异常发生时硬件自动保存 MSR (Machine Status Register) <br />
执行 rfi 从异常返回时，处理器会将 SRR1 值恢复入 MSR<br />
<br />
<br />
<strong>3.3 CSRR0</strong> (Critical Save/Restore Register 0)<br />
<br />
64 bit，用于 Critical Exception (0, 1, 12, 15)<br />
其它与 SRR0 同，只是该类型异常使用指令 rfci 返回<br />
<br />
<br />
<strong>3.4 CSRR1</strong> (Critical Save/Restore Register 1)<br />
<br />
32 bit，用于 Critical Exception (0, 1, 12, 15)<br />
其它与 SRR1 同，只是该类型异常使用指令 rfci 返回<br />
<br />
<br />
<strong>3.5 DEAR</strong> (Data Exception Address Register)<br />
<br />
64 bit，用于记录访存失败的地址，类似 MIPS 之 BadVAddr 或 x86 之 CR2<br />
<br />
<br />
<strong>3.6 IVPR &amp; IVORi</strong><br />
<br />
64 bit &amp; 32 bit，异常入口寄存器<br />
<br />
<br />
<strong>3.7 ESR</strong> (Exception Syndrome Register)<br />
<br />
<img class="blogimg" border="0" src="http://hiphotos.baidu.com/comcat/pic/item/229ad1c2be8f4b18e4dd3b82.jpg" small="0" alt="" /><br />
<br />
<strong><br />
4. 完整的例子<br />
</strong><br />
以外设中断为例：<br />
<br />
a. 外部中断控制器通过中断引脚 (#int) 触发处理器进入 External Input 异常<br />
b. 保存当前 PC + 4 入 SRR0 （异步，无需重新执行之，故下一条指令即可）<br />
c. 保存当前 MSR 入 SRR1，保持 MSR 之 CE, DE, ME，其余清除<br />
d. 跳转到入口 IVPR + IVOR4 处 （interrupt handler 之所在）<br />
e. interrupt handler 使用指令 rfi 返回，处理器在执行该指令时自动把 SRR1 恢复入 MSR，SRR0 入 PC<br />
<br />
<br />
<br />
最后给一个 BOOKE 之异常的全家福：<br />
<br />
<img class="blogimg" border="0" src="http://hiphotos.baidu.com/comcat/pic/item/ed2da8d148da77159a502789.jpg" small="0" alt="" /><br />
<br />
<br />
<strong>参考文献：</strong><br />
<br />
[1] Book E: Enhanced PowerPC Architecture, V1.0, 2002.5<br />]]></description></item></channel></rss>