当前版本: 1.0
完成日期: 2009-05-27
作者: Jack Tan
Email: jiankemeng@gmail.com
版权声明: 转载请注明出处,商业用途请联系作者
SPARC 的文档里将异常和中断统称为 Trap。一个 Trap 是指对异常或中断的响应行为。
1. Trap 概述
SPARC 有 3 个运行级别:用户态,特权级 (Privileged),超级特权级 (Hyperprivileged)。
OS 运行于特权级。为支持系统虚拟化,SUN 在 2005 年修订了 SPARC 的 Specification 引入了超级特权级 (Hyperprivileged) 用于运行Hypervisor
SPARC 下指定处理器状态的两个关键寄存器是 PSTATE 和 HPSTATE,分别对应于 Privileged 和 Hyperprivileged
为了良好地支持 Trap 嵌套,SPARC 引入了一个 Trap Level 寄存器,用于指定当前 Trap 的深度,正常运行的处理器 TL = 0, 处理器进入一个 Trap,TL 则加 1,SPARC 规范要求:兼容的实现得支持最少 4 个 Trap 嵌套 (用这个表示:MAXTL >= 4)
当一个 Trap 发生时,硬件做的反应是:
a. 自动保存 PC, NPC, (CCR, ASI, PSTATE, CWP) 寄存器到 TPC, TNPC, TSTATE (CCR, ASI, PSTATE, CWP 一起保存在 TSTATE)
b. 将 PSTATE 置为 privileged 模式
c. 跳转到 Trap vector 入口
因此与 Trap 相关的寄存器有:
TL, Trap Level 寄存器,指定当前处理器的 Trap Level,0 为正常模式
TSTATE, Trap State 寄存器,实现有 MAXTL 个
TT, Trap Type 寄存器,实现有 MAXTL 个
TPC, Trap Program Counter 寄存器,实现有 MAXTL 个
TNPC, Trap Next Program Counter 寄存器,实现有 MAXTL 个
因为 PC, NPC, (CCR, ASI, PSTATE, CWP) 都是硬件自动保存,因此支持多少层的 Trap 嵌套硬件就得实现相应数目的 TPC, TNPC,TT 和 TSTATE
Trap 发生时的处理器运行级别,位于 PSTATE[priv] 和 HPSTATE[hpriv]
SPARC 将 Reset, Error, Debug 的状态抽取出来,合称为 RED_state,用于对系统重启,硬件出错处理,处理器调试进行特殊的“照顾”。正常运行的状态称为 execute_state。当以下 Trap 出现时,处理器进入 RED_state:
POR (Power-On reset)
WDR (Watchdog Reset)
XIR (Externally Initiated Reset)
SIR (Software-Initiated Reset)
2. Trap 向量入口
2.1 正常运行时的向量入口
正常的运行状态 (execute_state),Trap 的向量入口由一个寄存器指定
使处理器进入特权级的 Trap,其向量入口由 TBA (Trap Base Address) 寄存器指定
使处理器进入超级特权级的 Trap,其向量入口由 HTBA (Hyperprivileged Trap Base Address) 指定
TBA 寄存器的位 63 ~ 15 为向量入口的基地址;
位 13 ~ 5 对应于 TT 寄存器,即不同类型的 Trap,其向量入口偏移也不一样;
TBA[4:0] 始终为 0,即向量入口是 32 字节对齐,每个 Trap handler 的初始长度是 32 字节,8 条指令
SPARC 还根据 Trap Level 的值,使用不同的向量入口,即:TL = 0 时,TBA[14] = 0; TL > 0 时,TBA[14] = 1;
2.2 RED_state 向量入口
因为 Reset, Error 及 Debug 的特殊性,当处理器位于 RED_state 状态时,其使用一个固定的向量入口基地址 RSTVaddr = 0xFFFF FFFF F000 0000,这是一个物理地址,位于物理地址空间的高 256MB。
RED_state 下的 Trap 偏移为:
Reserved 0×00
POR (Power-On reset) 0×20
WDR (Watchdog Reset) 0×40
XIR (Externally Initiated Reset) 0×60
SIR (Software-Initiated Reset) 0×80
Other 0xA0
因此上电启动时,UltraSPARC 处理器是从 0xFFFF FFFF F000 0020 处取第一条指令的,则 Bootloader 也应该位于该地址处
3. Trap 向量类型
Trap 的类型由 TT 寄存器指定,这是一个 9 bits 的寄存器,因此其最多可描述 2^9 = 512 种 Trap
但实际中,一个处理器要不了这么多的 Trap,以下择其要者 (SPARC V9):
TT Description
——————————————————
0×001 power on reset
0×005 RED state exception
0×008 instruction access exception
0×009 instruction access MMU miss
0x00a instruction access error
0×010 illegal instruction
0×011 privileged opcode
0×024 – 27 clearn window
0×030 data access exception
0×031 data access MMU miss
0×032 data access error
0×033 data access protection
0×034 mem address not aligned
0×041 – 04F interrupt level n (n = 1 ~ 15)
0×080 – 09F spill n normal (n = 0 ~ 7)
0x0A0 – 0BF spill n other (n = 0 ~ 7)
0x0C0 – 0DF fill n normal (n = 0 ~ 7)
0x0E0 – 0FF fill n other (n = 0 ~ 7)
0×100 – 17F trap instruction
为加快 TLB 相关 Trap 的处理, UltraSPARC 还引入了:
0×064 fast instruction access MMU miss
0×068 fast data access MMU miss
0x06C fast data access protection
SPARC V7 (32bit) 的:
Exception or Interrupt Request Priority TT
————————————————————-
reset 1 (see text)
data_store_error 2 0x2B
instruction_access_MMU_miss 2 0x3C
instruction_access_error 3 0×21
r_register_access_error 4 0×20
instruction_access_exception 5 0×01
privileged_instruction 6 0×03
illegal_instruction 7 0×02
fp_disabled 8 0×04
cp_disabled 8 0×24
unimplemented_FLUSH 8 0×25
watchpoint_detected 8 0x0B
window_overflow 9 0×05
window_underflow 9 0×06
mem_address_not_aligned 10 0×07
fp_exception 11 0×08
cp_exception 11 0×28
data_access_error 12 0×29
data_access_MMU_miss 12 0x2C
data_access_exception 13 0×09
tag_overflow 14 0x0A
division_by_zero 15 0x2A
trap_instruction 16 0×80 – 0xFF
interrupt_level_15 17 0x1F
interrupt_level_14 18 0x1E
interrupt_level_13 19 0x1D
interrupt_level_12 20 0x1C
interrupt_level_11 21 0x1B
interrupt_level_10 22 0x1A
interrupt_level_9 23 0×19
interrupt_level_8 24 0×18
interrupt_level_7 25 0×17
interrupt_level_6 26 0×16
interrupt_level_5 27 0×15
interrupt_level_4 28 0×14
interrupt_level_3 29 0×13
interrupt_level_2 30 0×12
interrupt_level_1 31 0×11
impl.-dependent exception impl.-dep. 0×60 – 0x7F