gpt4 book ai didi

Linux 分段

转载 作者:太空狗 更新时间:2023-10-29 12:40:35 31 4
gpt4 key购买 nike

最近,我读了一本名为Understanding the linux kernel 的书。有一句话让我很困惑。谁能给我解释一下?

As stated earlier, the Current Privilege Level of the CPU indicateswhether the processor is in User or Kernel Mode and is specified bythe RPL field of the Segment Selector stored in the cs register.Whenever the CPL is changed, some segmentation registers must becorrespondingly updated. For instance, when the CPL is equal to 3(User Mode), the ds register must contain the Segment Selector of theuser data segment,but when the CPL is equal to 0, the ds register must contain the Segment Selector of the kernel data segment.

A similar situation occurs for the ss register. It must refer to aUser Mode stack inside the user data segment when the CPL is 3, and itmust refer to a Kernel Mode stack inside the kernel data segment whenthe CPL is 0. When switching from User Mode to Kernel Mode, Linuxalways makes sure that the ss register contains the Segment Selectorof the kernel data segment.

When saving a pointer to an instruction or to a data structure, thekernel does not need to store the Segment selector component of thelogical address, because the ss register contains the current SegmentSelector.

As an example, when the kernel invokes a function, it executes a callassembly language instruction specifying just the Offset component ofits logical address; the Segment Selector is implicitly selected asthe one referred to by the cs register. Because there is just onesegment of type “executable in Kernel Mode,” namely the code segmentidentified by __KERNEL_CS, it is sufficient to load __KERNEL_CS intocs whenever the CPU switches to Kernel Mode. The same argument goesfor pointers to kernel data structures (implicitly using the dsregister), as well as for pointers to user data structures (the kernelexplicitly uses the es register).

我的理解是ss 寄存器包含指向堆栈底部的段选择器。 ss 寄存器与指向影响数据结构的指令的指针有什么关系吗?如果没有,为什么要在这里提及它?

最佳答案

终于把那段话的意思说清楚了。实际上,这段描述演示了分段在 Linux 中是如何工作的。它确实具有隐含的比较对象——那些系统利用分段而不是分页。这些系统如何运作?每个进程在其逻辑地址中都有不同的段选择器,这些段选择器指向全局描述符表中的不同条目。每个片段不一定需要具有相同的碱基。在那种情况下,当您保存指向指令或数据结构的指针时,您确实必须处理它的段基址。请注意,每个逻辑地址都有一个 16 位段选择器和一个 32 位偏移量。如果只保存偏移量,就不可能再次找到那个指针,因为 GDT 中有很多不同的段。对于 Linux,情况有所不同。所有段选择器都有相同的基数 0。这意味着指针的偏移量足够特殊,可以从内存中获取它。你可能会问,当有很多进程在那里运行时,它是否有效?有用!请记住,每个进程都有其页表,它具有将相同地址映射到不同物理地址的神奇力量。感谢所有关心这个问题的人!

关于Linux 分段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855720/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com