Segmentation in Hardware

介绍了硬件中的段

从80286开始,英特尔微处理器在两种不同的模式下(实模式和保护模式)进行地址转换。下一节讲述保护模式的地址转换。实模式用于处理器的向下兼容,并且允许操作系统的引导程序。
(Starting with the 80286 model, Intel microprocessors perform address translation in two different ways called real mode and protected mode . We’ll focus in the next sections on address translation when protected mode is enabled. Real mode exists mostly to maintain processor compatibility with older models and to allow the operating system to bootstrap(引导程序) (see Appendix A for a short description of real mode). )

2.2.1. Segment Selectors and Segmentation Registers(段选择器和段寄存器)

(段标识符是16位的域,被称为“段选择器”, 此时offset是32位域)
A logical address consists of two parts: a segment identifier and an offsetthat specifies the relative address within the segment. The segment identifier is a 16-bit field called the Segment Selector (see Figure 2-2), while the offset is a 32-bit field. We’ll describe the fields of Segment Selectors in the section “Fast Access to Segment Descriptors” later in this chapter.
figure 2-2
To make it easy to retrieve(重新得到) segment selectors quickly, the processor provides segmentation registers(段寄存器) whose only purpose is to hold Segment Selectors(段选择子); these registers are called cs , ss , ds ,es , fs , and gs.
(三个段寄存器有特殊用途)Three of the six segmentation registers have specific purposes:

cs
The code segment register, which points to a segment containing program instructions
ss
The stack segment register, which points to a segment containing the current program stack
ds
The data segment register, which points to a segment containing global and static data

剩下的寄存器是通用目标的寄存器,可能会指向任意的数据段(The remaining three segmentation registers are general purpose and may refer to arbitrary(任意的) data segments.)

cs寄存器的特殊作用

cs寄存器的重要作用:包含了2位的域用于指定CPU的当前特权级别。
0代表最高特权级
3代表最低特权级
Linux只有2个级别0或者3,相应的被称为内核模式和用户模式

(The cs register has another important function: it includes a 2-bit field that specifies the Current Privilege Level (CPL) of the CPU. The value 0 denotes the highest privilege level, while the value 3 denotes the lowest one. Linux uses only levels 0 and 3, which are respectively called Kernel Mode and User Mode.)

2.2.2. Segment Descriptors

每个段使用8位段描述符来表示,段描述符用于描述段的特征。段描述符存储在GDT或者LDT中。

(Each segment is represented by an 8-byte Segment Descriptor that describes the segment characteristics. Segment Descriptors are stored either in the Global Descriptor Table (GDT ) or in the Local Descriptor Table(LDT).)

通常只有一个GDT,每个进程可以拥有自己的LDT(当进程需要在GDT的段之外创建额外的段。GDT的尺寸和地址被保存在寄存器gdtr中,当前是用的LDT的地址和尺寸保存在ldtr中)

Usually only one GDT is defined, while each process is permitted to have its own LDT if it needs to create additional segments besides those stored in the GDT. The address and size of the GDT in main memory are contained in the gdtr control register, while the address and size of the currently used LDT are contained in the ldtr control register.

下图演示了段描述符的形式。各个域的意思在接下来的表格2-1中。

(Figure 2-3 illustrates the format of a Segment Descriptor; the meaning of the various fields is explained in Table 2-1.)

figure2-3

figure 2-3

table 2-1

Fieldname Description
Base段第一个字节的线性地址(Contains the linear address of the first byte of the segment.)
G间隔标志:如果为0,段尺寸用字节表示;否则用4096字节的整数倍来表示(Granularity flag: if it is cleared (equal to 0), the segment size is expressed in bytes; otherwise, it is expressed in multiples of 4096 bytes.)
Limit保存段的最后一个内存单元的偏移量,取决于段长度。如果G为0,范围是1byte~1mb,否则,范围是4KB~4GB(Holds the offset of the last memory cell in the segment, thus binding the segment length. When G is set to 0, the size of a segment may vary between 1 byte and 1 MB; otherwise, it may vary between 4 KB and 4 GB.)
S系统标志,如果被清除,则该段是存放关键数据结构如LDT的系统段。否则是一般的代码或者数据段(System flag: if it is cleared, the segment is a system segment that stores critical data structures such as the Local Descriptor Table; otherwise, it is a normal code or data segment.)
Type特定段的类型和其访问权限(Characterizes the segment type and its access rights .)
DPL描述符特权级:用于限制段的访问权限。其表示请求该段的最小CPU特权级。当DPL为0,只有内核模式才可以访问(CPL值为0).DPL为3,任何CPL值都可以访问(Descriptor Privilege Level: used to restrict accesses to the segment. It represents the minimal CPU privilege level requested for accessing the segment. Therefore, a segment with its DPL set to 0 is accessible only when the CPL is 0 that is, in Kernel Mode while a segment with its DPL set to 3 is accessible with every CPL value.)
P段存在标志,如果为0:该段当前没有存储在主存中。Linux总是设置该值为1,因为Linux没有和disk交换整个段(Segment-Present flag : is equal to 0 if the segment is not stored currently in main memory. Linux always sets this flag (bit 47) to 1, because it never swaps out whole segments to disk.)
D or B是D还是B村绝育该段包含的是代码还是数据,在两种情况下有一些不同之处。如果地址和段偏移量都是32位长,那么值为1.如果这些都是16位的,那么值为0(Called D or B depending on whether the segment contains code or data. Its meaning is slightly different in the two cases, but it is basically set (equal to 1) if the addresses used as segment offsets are 32 bits long, and it is cleared if they are 16 bits long (see the Intel manual for further details).)
AVL用于操作系统,但是对于Linux没有用(May be used by the operating system, but it is ignored by Linux.)

因为有很多段(segment),所以有很多类型的段描述符。下面展示在Linux中最广泛使用的几种:

Code Segment Descriptor

Indicates that the Segment Descriptor refers to a code segment; it may be included either in the GDT or in the LDT. The descriptor has the S flag set (non-system segment).

Data Segment Descriptor

Indicates that the Segment Descriptor refers to a data segment; it may be included either in the GDT or in the LDT. The descriptor has the S flag set

堆栈段是使用数据段实现的。Stack segments are implemented by means of generic data segments.

Task State Segment Descriptor (TSSD)

用于任务状态段,任务状态段是用于保存处理器寄存器的内容.TSSD仅仅可以出现再GDT中,相应的type域的值要为11或者9,取决于相应的进程是否运行在CPU上。S flag = 0;

(Indicates that the Segment Descriptor refers to a Task State Segment (TSS) that is, a segment used to save the contents of the processor registers (see the section “Task State Segment” in Chapter 3); it can appear only in the GDT. The corresponding Type field has the value 11 or 9, depending on whether the corresponding process is currently executing on a CPU. The S flag of such descriptors is set to 0.)

Local Descriptor Table Descriptor (LDTD)

指示包含LDT的段,只能存在于GDT中。相应的type域,值为2.s flag = 0.
下一节演示80x86处理器如何决定段描述符是否存在GDT或者LDT中。

(Indicates that the Segment Descriptor refers to a segment containing an LDT; it can appear only in the GDT. The corresponding Type field has the value2. The S flag of such descriptors is set to 0. The next section shows how 80 x 86 processors are able to decide whether a segment descriptor is stored in the GDT or in the LDT of the process.)

2.2.3. Fast Access to Segment Descriptors

回顾16位段选择器和32位偏移量的逻辑地址,段寄存器仅仅保存段选择器

(原文)We recall that logical addresses consist of a 16-bit Segment Selector and a 32-bit Offset , and that segmentation register s store only the Segment Selector.

为了加速逻辑地址到线性地址的转换,80x86处理器提供了额外的不可编程的寄存器,该寄存器是不能被程序员设置给6个可编程段寄存器的。每个不可编程寄存器包含8字节由段选择子特定的段描述符,段选择子保存在相应的段寄存器中。每次段选择子加载到段寄存器中,相应的段描述符从内存加载到匹配的不可编程寄存器中。因此,逻辑地址的转换不需要访问主存中GDT或者LDT,处理器可以直接访问包含段描述符的CPU寄存器。对GDT和LDT的访问只有在段寄存器内容改变时发生。
见下图:
figure 2-4

(原文)To speed up the translation of logical addresses into linear addresses, the 80 x 86 processor provides an additional nonprogrammable registerthat is, a register that cannot be set by a programmer for each of the six programmable segmentation registers. Each nonprogrammable register contains the 8-byte Segment Descriptor specified by the Segment Selector contained in the corresponding segmentation register. Every time a Segment Selector is loaded in a segmentation register, the corresponding Segment Descriptor is loaded from memory into the matching nonprogrammable CPU register. From then on, translations of logical addresses referring to that segment can be performed without accessing the GDT or LDT stored in main memory; the processor can refer only directly to the CPU register containing the Segment Descriptor. Accesses to the GDT or LDT are necessary only when the contents of the segmentation registers change (see Figure 2-4).

任何段选择子包含的三个域如下:
(Any Segment Selector includes three fields that are described in Table 2-2.)

Fieldname Description
index鉴定包含在GDT和LDT段描述符—Identifies the Segment Descriptor entry contained in the GDT or in the LDT.
TI表指示器:指定段描述符是包含在GDT(TI = 0)还是LDT中(TI = 1)—Table Indicator : specifies whether the Segment Descriptor is included in the GDT (TI = 0) or in the LDT (TI = 1).
RPL请求者特权级:制定当前的CPU特权级,当相应的段选择子被加载到cs寄存器时。当访问数据段的时候,其也被用来唤醒处理器特权级—Requestor Privilege Level : specifies the Current Privilege Level of the CPU when the corresponding Segment Selector is loaded into the cs register; it also may be used to selectively weaken the processor privilege level when accessing data segments

因为段描述符是8字节,其在GDT或者LDT中响应的地址由段选择子的13位index域乘上8获得的。例如,GDT地址是0x00020000,段选择子的index是2,所以相应的段描述符地址为 0x00020000 + (2 * 8),也就是)0x00020010

(原文)Because a Segment Descriptor is 8 bytes long, its relative address inside the GDT or the LDT is obtained by multiplying the 13-bit index field of the Segment Selector by 8. For instance, if the GDT is at 0x00020000 (the value stored in the gdtr register) and the index specified by the Segment Selector is 2, the address of the corresponding Segment Descriptor is 0x00020000 + (2 x 8) , or 0x00020010 .

GDT的第一个实体总是被设置为0.这能确保逻辑地址的null段描述符能被认为是非法的,从而造成处理器异常。段描述符的最大数字被保存在GDT中。

English Text:
The first entry of the GDT is always set to 0. This ensures that logical addresses with a null Segment Selector will be considered invalid, thus causing a processor exception. The maximum number of Segment Descriptors that can be stored in the GDT is 8,191 (i.e., 2 13 -1).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值