AIFeed

理解“你不被期望理解这一点”

Translation(model translation, not the source's own text)

理解“你不被期望理解这个”

很多很多很多年前,我将 Unix V6 移植到了 Vax 上,它作为 VMS 下的虚拟机在 supervisor/user 模式下运行(VMS 通常在这些模式下运行 shell)——据我所知这些代码已经湮没在历史中,不过一篇 DECUS 论文留存了下来。后来我还移植了 V7。

在这个过程中,我经常在上下文切换代码中遇到那句臭名昭著的话“你不被期望理解这个”,而我确实没理解它……后来有一天,我从另一条路径再次接触到那段代码,一切就显而易见了……让我来解释一下到底是怎么回事,下面是一次深入剖析。

V6 Unix 将每个进程的状态保存在两个地方:进程表中的一个小区块,以及通过 MMU 映射到众所周知的内核虚拟地址的 'udot' 结构,该结构被引用为“u.”,其中还包含内核栈。在所有运行中的进程里,这个结构总是被映射到相同的地址,建立这个映射是每次进程切换的核心部分。

V6 使用了一种略显巴洛克的方式来保存和恢复运行中进程的状态,在 V7 中他们改用 save()/restore(),其工作方式类似于我们更熟悉的 setjmp()/longjmp()。这深深依赖于 V6 PDP-11 调用约定中的细节——savu() 保存足够的状态以便返回到它,它会保存栈指针(r6)和帧指针(r5),但不碰 r2-r4。


        _savu:
	bis     $340,PS		// turn off interrupts
	mov     (sp)+,r1	// pull the PC off the stack
	mov     (sp),r0		// pull the address to store off the stack
	mov     sp,(r0)+	// now store the SP (as it was then savu was called)
	mov     r5,(r0)+	// and store r5 too
	bic     $340,PS		// turn interrupts back on
	jmp     (r1)		// and return
      

retu() 和 aretu() 基本相同,两者都恢复当前状态,但调用它们的例程会如履薄冰,因为栈已经在它们脚下发生了改变,帧指针也是如此(因此任何存储在栈上的局部变量也一样),在你调用它们之后,生活变得有点吓人,必须小心谨慎,尤其是在中断被打开之后,我们更是如履薄冰……retu() 与 aretu() 的区别在于,retu() 还会将 udot 映射到内存的最后一页——retu 总是传入 udot 的物理地址,这里有一点魔法,udot 的前两个字同时也是 u.u_rsav,也就是 retu() 真正返回的地方——它们在这里稍有展开:


        _aretu:
	bis     $340,PS		// turn interrupts off
	mov     (sp)+,r1	// load pc
	mov     (sp),r0		// get address of save area
	mov     (r0)+,sp	// restore stack pointer
	mov     (r0)+,r5	// restore frame pointer
	bic     $340,PS		// turn interrupots on
	jmp     (r1)		// return to caller

_retu:
	bis     $340,PS		// turn interrupts off
	mov     (sp)+,r1	// load pc
	mov     (sp),KISA6	// get physical address of u. into MMU
	mov     $_u,r0		// get kernel virtual address of u.u_rsav in r0
	mov     (r0)+,sp	// restore stack pointer
	mov     (r0)+,r5	// restore frame pointer
	bic     $340,PS		// turn interrupots on
	jmp     (r1)		// return to caller
      

这里还有一点魔法:r2-r4 未被触碰,关于这一点后面再说。

那么,这就是那段臭名昭著的上下文切换代码(我省略了选择新进程运行的部分)——注意 swtch() 声明时并不返回值,但在例程末尾它显式地返回“1”。这并不算特别不寻常,当时的 C 语言没有 void,类型系统也很简陋,如果你不声明什么,它默认就是“int”。同样在 V7 之前,C 语言将“a += 1”写成“a =+ 1”(有歧义),所以你会发现代码中出现“rp->p_flag =& ~SSWAP;”这样的写法,这在当时是正常的。当时出于性能考虑,告诉编译器你想把哪些变量放在寄存器而不是栈上,是很常见的做法,因为编译器并不聪明。现代编译器如果你喂给它带有 register 关键字的代码,它会报怨。


        swtch()
{
	static struct proc *p;
	register i, n;
	register struct proc *rp;
      

文章标题:理解“你不被期望理解这个。”

正文: if(p == NULL) p = &proc[0];

/*

  • Remember stack of caller

/ savu(u.u_rsav); /

  • Switch to scheduler's stack

*/ retu(proc[0].p_addr);

..... some code to choose a new process to run, points rp at it

/* Switch to stack of the new process and set up

  • his segmentation registers.

/ retu(rp->p_addr); sureg(); /

  • If the new process paused because it was
  • swapped out, set the stack level to the last call
  • to savu(u_ssav). This means that the return
  • which is executed immediately after the call to aretu
  • actually returns from the last routine which did
  • the savu.

*

  • You are not expected to understand this.

/ if(rp->p_flag&SSWAP) { rp->p_flag =& ~SSWAP; aretu(u.u_ssav); } /

  • The value returned here has many subtle implications.
  • See the newproc comments.

*/ return(1); }


        
那么这里到底发生了什么,让我们先暂时忽略“你不被期望理解这个。”之后的 if 语句,上下文切换是这样工作的:

- savu(u.u_rsav); 在 udot 的开头保存当前进程的状态

- retu(proc[0].p_addr); 将上下文切换到进程 0

- .... 一些代码选择下一个要运行的进程,并将 rp 指向它在进程表中的条目

- retu(rp->p_addr); 将上下文切换到新的 udot,并从新的 udot 加载其上下文——此时我们正处在上述的“薄冰”之上

- sureg() 为新进程设置用户空间 MMU 映射(在薄冰之上 sureg() 最好小心一点……)

- 然后我们向所有那些从不使用返回值的 swtch() 调用返回 1

现在这里的冰其实并没有那么薄,我们把栈指针和帧指针恢复到了执行 savu(u.u_rsav); 时 swtch() 内部的状态;期间出现的任何中断都不会破坏我们正在使用的栈。此外,我们指向的局部变量正是我们最后一次执行 savu() 时所处位置使用的那些。

现在来谈谈那段我们不该理解的部分:如果对于新进程,进程标志 SSWAP 被置位,我们就清除它并执行 aretu(u.u_ssav);——返回到交换保存地址然后返回 1 …… 那么 SSWAP 是在哪里被置位的呢?答案是每当一个进程因为内存不足而被换出时,此时我们刚刚执行了 savu(u.u_ssav); 将当前栈状态保存到一个特殊的交换保存状态。大多数情况下这是从 fork() 调用的 newproc() 例程——newproc 对于 fork() 产生的新进程返回 1,对旧进程返回 0。

相关代码如下:

      

newproc() { .... savu(u.u_rsav); ....

a2 = malloc(coremap, n); /*

  • If there is not enough core for the
  • new process, swap out the current process to generate the
  • copy.

*/ if(a2 == NULL) { rip-&p_stat = SIDL; rpp-&p_addr = a1; savu(u.u_ssav); xswap(rpp, 0, 0); rpp-&p_flag =| SSWAP; rip-&p_stat = SRUN; } else {

.....

return(0); }


        
当我们执行那段我们不被期望理解的代码时会发生什么?它只会被一个刚刚换入且该标志被置位的新进程触发。

      

*

  • You are not expected to understand this.

*/ if(rp->p_flag&SSWAP) { rp->p_flag =& ~SSWAP; aretu(u.u_ssav); } return(1);

于是我们执行 aretu(u.u_ssav); 这会将栈指针和帧指针重新加载到上面 newproc() 中执行 savu(u.u_ssav); 时的位置,xswap() 已经在那一刻把 udot(由新进程和旧进程共享)写到了一个交换文件中,那个交换文件已经被重新加载,而现在我们刚刚切换到那个新的 udot)aretu() 将栈指针和帧指针重新加载到它们在 newproc() 中的位置,然而 PC 仍然指向 swtch() 中的代码,随后它执行 return(1)——最终向调用 newproc() 的一方(要么是 fork(),要么是创建 init 时的 main())返回 1。

文章标题:理解“你不被期望理解这个。”

正文: 所以,在调用 aretu() 之后,我们也处于危险境地,但因为整个过程中栈都处于可安全响应中断的状态,所以 retu(rp->p_addr);(本质上就是 retu(u.u_rsav); 与 newproc() 中的 savu(u.u_rsav); 相对应)并不低于 aretu(u.u_ssav); 所跳转到的位置。

最后,让我们谈谈那段我们不被期望理解的代码中的变量 rp。它是一个在调用 retu() 之后被访问的局部变量,但它是寄存器变量——这里面有些玄机,可能藏在 swtch() 开头声明顺序里。我的猜测是编译器把它放进了 r4(这是被调用者保存寄存器),r2-r4 不会被 savu 和 retu/aretu 触碰,但 r2-r3 是调用者保存寄存器。如果它们被 swtch() 保存和恢复,就会从新栈上取到垃圾数据——或者 r4 存放着 rp,而其余寄存器变量不再活跃,因此要么它们在调用 retu/aretu 时没有被保存/恢复,要么没人在意,因为在第一次 retu() 调用之后它们就不再被使用了——我倾向于后者,因为编译器当时大概还不够聪明,做不了生命周期分析。

就是这样,所有血淋淋的细节都在这里了。如果你发现这里有错误,请告诉我(那已经是 45 年前的事了)。

Paul Campbell [email protected] 2026

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗