前言
之前在学习x86架构时,在代码段中有一个点让我感到困惑,就是type field 11位。这个之前一直似懂非懂,说不理解吧知道这么个玩意,说理解吧又不知道其具体干嘛,感觉是方便系统调用加快速度,尤其当遇到权限问题,自己就糊里糊涂,这篇文章让自己彻底分析明白这个问题。
Intel手册说明
下面来引用intel手册中对这问题的描述(卷三 3.4.5.1节)
Code segments can be either conforming or nonconforming. A transfer of execution into a more-privileged conforming segment allows execution to continue at the current privilege level. A transfer into a nonconforming segment at a different privilege level results in a general-protection exception (#GP), unless a call gate or task gate is used.
这上面讲:对于一致代码段,其可以由低权限调用高权限中的代码(反过来不行);对于非一致代码段,其不同权限调用会触发GP一样,必须通过调用们或任务门。
All data segments are nonconforming, meaning that they cannot be accessed by less privileged programs or procedures (code executing at numerically higher privilege levels). Unlike code segments, however, data segments can be accessed by more privileged programs or procedures (code executing at numerically lower privilege levels) without using a special access gate.
我们现在来讲这部分的是”所有数据段都是非一致代码段“,低权限程序无法访问,但是其高权限程序可以直接访问低权限代码中的数据。
一致代码段在实际的使用
一致代码段是为了一些代码在不管零环和三环都可以很方便的调用而设计的一种机制,这与windows系统中的SSDT表中的调用不是一回事。举一个例子,我们现在设计了一个math.lib来完成运算任务,当三环和零环都需要调用这个库来完成计算任务时,其最好的方式就是使用一致代码段。
&