今天在复习sparc体系结构的相关知识时,发现自己对寄存器窗口的上溢和下溢处理代码有点理不清了,特此记录一下,SPARC体系结构相关知识参见《 http://www.sics.se/~psm/sparcstack.html》一文,这里仅分析上溢和下溢的处理函数。
1 上溢陷阱(Overflow Trap)处理函数
- /* a SAVE instruction caused a trap */
- window_overflow:
- /* rotate WIM on bit right, we have 8 windows */
- mov %wim,%l3
- sll %l3,7,%l4
- srl %l3,1,%l3
- or %l3,%l4,%l3
- and %l3,0xff,%l3
- /* disable WIM traps */
- mov %g0,%wim
- nop; nop; nop
- /* point to correct window */
- save
- /* dump registers to stack */
- std %l0, [%sp + 0]
- std %l2, [%sp + 8]
- std %l4, [%sp + 16]
- std %l6, [%sp + 24]
- std %i0, [%sp + 32]
- std %i2, [%sp + 40]
- std %i4, [%sp + 48]
- std %i6, [%sp + 56]
- /* back to where we should be */
- restore
- /* set new value of window */
- mov %l3,%wim
- nop; nop; nop
- /* go home */
- jmp %l1
- rett %l2
结合上面代码,拿一个具体的实例来进行分析。
Example: CWP = 3, WIM = 2, 此时,执行SAVE操作。
执行SAVE操作时,做的第一件事就是判断(CWP - 1) == WIM ? 如果相等,就会触发Overflow Trap,具体代码如上:
(1)首先,计算正确的WIM值,即第4-第8行代码(此时,因为Overflow Trap的原因, CMP = 2)
(2)执行SAVE操作 (SAVE引起CWP--, 所以,CWP = 1)
(3)一系列的std,目的是将CWP=1窗口的内容写到内存中
(4)执行restore操作 (restore操作引起CWP++, 此时,CWP = 2)
(5)执行rett操作 (rett引起CWP++, 此时,CWP = 3)
经过上面5步后,CWP = 3, WIM = 1,所以再次执行SAVE操作时, CWP - 1 = 2,不等于WIM,SAVE操作执行成功。
2. 下溢陷阱(Underflow Trap)处理函数
- /* a RESTORE instruction caused a trap */
- window_underflow:
- /* rotate WIM on bit LEFT, we have 8 windows */
- mov %wim,%l3
- srl %l3,7,%l4
- sll %l3,1,%l3
- or %l3,%l4,%l3
- and %l3,0xff,%l3
- /* disable WIM traps */
- mov %g0,%wim
- nop; nop; nop
- /* point to correct window */
- restore
- restore
- /* dump registers to stack */
- ldd [%sp + 0], %l0
- ldd [%sp + 8], %l2
- ldd [%sp + 16], %l4
- ldd [%sp + 24], %l6
- ldd [%sp + 32], %i0
- ldd [%sp + 40], %i2
- ldd [%sp + 48], %i4
- ldd [%sp + 56], %i6
- /* back to where we should be */
- save
- save
- /* set new value of window */
- mov %l3,%wim
- nop; nop; nop
- /* go home */
- jmp %l1
- rett %l2
Example: CWP = 3, WIM = 4, 此时,执行restore操作。
类似save操作,restore操作要干的第一件事也是检查(CWP + 1)== WIM ?如果是, 则触发Underflow Trap, 进入Underflow Trap处理函数,具体代码如上:
(1)进入window_underflow (此时,由于是通过underflow trap进来的,所以, CWP = 2)
(2)类似的,计算正确的WIM值
(3)两次restore操作 (此时,CWP = 4, 因为加了两次)
(4)将内存中保存的值恢复到CWP=4的窗口中
(5)两次save操作 (此时, CWP = 2)
(6)rett操作 (此时,CWP = 3)
执行完上述6步之后,CWP = 3, WIM = 5,再次执行restore操作时,CWP+1 = 4, 成功执行。
3. 总结
这里最需要注意的是不论是Overflow还是Underflow,都是Trap,都会首先使CWP--,很过人不理解Underflow的两次restore,就是忘记了下溢的时候,CWP要首先减一。