UNIX 中的 panic 函数

Panic function in UNIX

我目前正在尝试从 UNIX OS 中获取函数 idle() 的某个版本。我有源代码,但我不擅长汇编语言(我最近一直在尝试改变)。有人可以帮助我更好地了解它的工作原理吗?

我试过搜索它,但没有找到有用的信息。 "Lion's Commentary on UNIX"这本书我也查过了,但是没有找到任何解释。

这是函数的源代码,that's link 是完整的源代码。

.globl  _idle
_idle:
    mov PS,-(sp)
    bic 0,PS
    wait
    mov (sp)+,PS
    rts pc

嗯,这是PDP 11/40汇编语言,在手册中有定义

让我们逐行分解:

.globl  _idle    # define a global symbol called idle
_idle:           # this is the label for the global symbol
    mov PS,-(sp) # push processor state onto stack
    bic 0,PS  # clear priority level bits - effectively enable all interrupts
    wait         # wait for an interrupt
    mov (sp)+,PS # pop processor state from stack
    rts pc       # return from function

The -(sp) and (sp)+ should be read as the equivalent to the C/C++ operators --sp and sp++.

所以,它有效地保存状态,清除优先级位,然后等待中断。一旦中断到达,它就会恢复状态并重新开始工作。

PS寄存器的内容定义见手册[2.3.2 Processor Status Word]一节

现在,wait 操作会由于各种原因而中断,其中最重要的是实时时钟中断,因此它会定期被唤醒以做更多的工作。

当您查看源代码时,有两个地方调用了 idle() 例程 - 一个来自恐慌处理程序,在一个无限循环中,另一个在 swtch 中,它在进程之间交换,当它找不到可运行的进程时进入空闲例程。