SPARC寄存器窗口上溢(overflow)和下溢分析(underflow)

今天在复习sparc体系结构的相关知识时,发现自己对寄存器窗口的上溢和下溢处理代码有点理不清了,特此记录一下,SPARC体系结构相关知识参见《 http://www.sics.se/~psm/sparcstack.html》一文,这里仅分析上溢和下溢的处理函数。
1 上溢陷阱(Overflow Trap)处理函数

  1. /* a SAVE instruction caused a trap */
  2. window_overflow:
  3. /* rotate WIM on bit right, we have 8 windows */
  4. mov %wim,%l3
  5. sll %l3,7,%l4
  6. srl %l3,1,%l3
  7. or %l3,%l4,%l3
  8. and %l3,0xff,%l3
  9. /* disable WIM traps */
  10. mov %g0,%wim
  11. nop; nop; nop
  12. /* point to correct window */
  13. save
  14. /* dump registers to stack */
  15. std %l0, [%sp + 0]
  16. std %l2, [%sp + 8]
  17. std %l4, [%sp + 16]
  18. std %l6, [%sp + 24]
  19. std %i0, [%sp + 32]
  20. std %i2, [%sp + 40]
  21. std %i4, [%sp + 48]
  22. std %i6, [%sp + 56]
  23. /* back to where we should be */
  24. restore
  25. /* set new value of window */
  26. mov %l3,%wim
  27. nop; nop; nop
  28. /* go home */
  29. jmp %l1
  30. 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)处理函数


  1. /* a RESTORE instruction caused a trap */
  2. window_underflow:
  3. /* rotate WIM on bit LEFT, we have 8 windows */
  4. mov %wim,%l3
  5. srl %l3,7,%l4
  6. sll %l3,1,%l3
  7. or %l3,%l4,%l3
  8. and %l3,0xff,%l3
  9. /* disable WIM traps */
  10. mov %g0,%wim
  11. nop; nop; nop
  12. /* point to correct window */
  13. restore
  14. restore
  15. /* dump registers to stack */
  16. ldd [%sp + 0], %l0
  17. ldd [%sp + 8], %l2
  18. ldd [%sp + 16], %l4
  19. ldd [%sp + 24], %l6
  20. ldd [%sp + 32], %i0
  21. ldd [%sp + 40], %i2
  22. ldd [%sp + 48], %i4
  23. ldd [%sp + 56], %i6
  24. /* back to where we should be */
  25. save
  26. save
  27. /* set new value of window */
  28. mov %l3,%wim
  29. nop; nop; nop
  30. /* go home */
  31. jmp %l1
  32. 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要首先减一。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值