如何在 sparc V8 处理器中设置 TBR?

How to set TBR in a sparc V8 processor?

如果这是一个非常基本的问题,我深表歉意,恐怕我不知道可以向谁或在哪里提出这样的问题。

我正在做一个 sparc v8 模拟器项目,目前我一直在处理陷阱。 V8 手册对我没有帮助,我不知道 'trap' 有什么 'trap number'.

The comments on this question give me some idea, but not the whole picture. This page 解释了陷阱入口,但我仍然遇到同样的问题。不知道给TBR赋什么初始值以及如何根据'trap number'.

计算TBR值

我从 TBR 寄存器设置为 0 开始。如果我的 ELF 文件有,比如 ta 1,要遵循哪些步骤?

How to set TBR in a sparc V8 processor?

执行此操作的指令在某些汇编器中被命名为 wrtbr value(例如 wrtbr %i5),在其他汇编器中被命名为 mov value, %tbr(例如 mov %i4, %tbr)。

只有操作系统可以改变TBR寄存器;尝试从用户模式(应用程序)访问此寄存器将导致类型 3 异常。

如果您使用的 Sparc 模拟器仅在应用程序级别模拟 CPU(几年前我写过类似的),则根本不支持此指令,因为只有 OS可以执行。

请注意,您只写入“TBR”寄存器的“TBA”字段(位 31...12);当 trap/interrupt/exception 发生时,“tt”字段(位 11...4)由 CPU 写入。

... what 'trap' has what 'trap number'.

“Sparc 体系结构手册第 8 版”在第 76 页的 table 7-1 中列出了“tt”(TBR 寄存器的位 11..4)的值:

0x01      Instruction access exception
0x02      Illegal instruction
0x03      Privileged instruction
0x04      Floating point disabled
0x05      Window overflow
0x06      Window underflow
0x07      Memory address not aligned
0x08      Floating point exception
0x09      Data access exception
0x0A      Tag overflow
0x0B      Watch point detected
0x11-0x1F Hardware interrupts
0x20      Register access error
0x21      Instruction access error
0x24      Coprocessor disabled
0x25      Unimplemented FLUSH
0x28      Coprocessor exception
0x29      Data access error
0x2A      Division by zero
0x2B      Data store error
0x2C      Data access MMU miss
0x3C      Instruction access MMU miss
0x60-0x7F CPU or hardware specific
0x80-0xFF "txx" instructions (example: "ta 0x25" => 0x80+0x25 = 0xA5)

I don't know what initial value to give TBR and how to calculate TBR value based on 'trap number'.

你必须实现 256 个中断处理程序,每个 4 条指令长度,并将这 256 个中断处理程序放在某个 4096 字节长度的内存区域,其地址是 4096 的倍数。

(因为一个真正的异常处理程序的长度超过4条指令,所以大多数4指令异常处理程序或多或少都是跳转到实际异常处理程序的指令。)

示例:您的 256 个中断处理程序位于地址 0x12345000。

您将此地址(示例中的 0x12345000)写入 TBR 寄存器。

如果出现陷阱,陷阱编号将乘以 16 并添加到该地址。结果是中断处理程序的地址。

示例:

执行ta 5指令时,出现陷阱号(5+0x80)

0x12345000+(5+0x80)*16 = 0x12345850.

CPU 将在地址 0x12345850 处执行陷阱处理程序。

假设一些 save 指令被执行并且内部寄存器中不再有 space。然后发生“window 溢出”(陷阱编号 5)。

0x12345000+5*16 = 0x12345050.

CPU 将在地址 0x12345050 处执行陷阱处理程序。

I'm starting with TBR register set to 0. If my ELF file has, say ta 1, what are the steps to follow?

这意味着 CPU 在地址 0+(0x80+1)*16 = 0x810 处执行代码。

但是,正如我已经说过的,您不能从应用程序访问 TBR 寄存器,而只能从 OS...