为什么在 xv6 中 gdtdesc 中有 sizeof(gdt)-1

Why in xv6 there's sizeof(gdt)-1 in gdtdesc

bootasm.S

.p2align 2                                # force 4 byte alignment
gdt:
  SEG_NULLASM                             # null seg
  SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff)   # code seg
  SEG_ASM(STA_W, 0x0, 0xffffffff)         # data seg

gdtdesc:
  .word   (gdtdesc - gdt - 1)             # sizeof(gdt) - 1
  .long   gdt                             # address gdt

这用于

lgdt gdtdesc

gdtdesc的第一个字不应该是gdt的字节大小吗?在本例中,它是 3*8=24,等于 gdtdesc - gdt。 为什么 gdtdesc - gdt - 1 在这里?

根据手册,lgdt 需要以字节为单位的 GDT 大小,但也将其描述为“限制”。最后一个字节的大小与地址之间的措辞不明确。 (这是一种允许更高限制而不包含 16 位限制的方法。)

但是 https://wiki.osdev.org/GDT_Tutorial 中的示例使用了 sizeof(gdt),所以这要么是 xv6 中的错误,要么是 osdev 教程中的错误。

https://wiki.osdev.org/Global_Descriptor_Table同意xv6,说“limit”是size-1,不像GDT教程。这是有道理的:

The size is the size of the table subtracted by 1. This is because the maximum value of size is 65535, while the GDT can be up to 65536 bytes (a maximum of 8192 entries). Further no GDT can have a size of 0.

如果要确认详情,请查看Intel或AMD的手册;他们希望在系统开发细节的某处澄清这一点,与 lgdt.

的指令集参考条目分开

或者你希望他们会。但不幸的是英特尔说:

Section 2.4.1 Global Descriptor Table Register (GDTR)

"the table limit specifies the number of bytes in the table".

这可能只是意味着写它的人是如此陷入限制 = 最后一个字节的偏移量 = 大小的想法,以至于他们甚至没有意识到这一点尚不清楚。段限制本身(在 GDT 条目中)也以这种方式工作,使用 0xFFFFF(粒度=页面)将限制指定为 4GiB 地址的最顶端 space,即无限制。