简体   繁体   中英

Will it be OK to access data through a data segment register after I disturb gdt table?

I had set the gdt table using this data

uint16_t gdt_table[][4] = {
    {   0,         0,      0,      0       },
    {   0xFFFF,    0x0000, 0x9a00, 0x00cf  },
    {   0xFFFF,    0x0000, 0x9200, 0x00cf  },
};

And I do mov $16, %eax ; mov %eax, %ds to load DS with a GDT entry.

Now I change the gdt_table, or just set it to zero (to disturb it) but don't reload %ds again.


These are my questions

  • I want to konw is it still ok for me to access data through %ds (after I disturb the gdt table)
  • Will cpu check gdt table every time I access data through %ds ?(So when will cpu check gdt table, only when mov? Or every time I access data through data segment register?)

I think cpu should check gdt table only when mov happened, and then load some information somewhere, as it can be more effective(by checking less often), is it right?

I want to konw is it still ok for me to access data through %ds(after I disturb the gdt table)

It's safe; unless something reloads %ds from the GDT later. It isn't necessarily uncommon for interrupt handlers to save "interrupted code's DS" and then load it again before returning to the interrupted code.

Will cpu check gdt table every time I access data through %ds?

For performance (to avoid repeatedly accessing GDT and doing protection checks) the CPU caches the (segment base, limit, attributes) information in "hidden" parts of the segment register. This information is also saved and reloaded during transitions to/from SMM and transitions between guest and hypervisor; so GDT isn't used for those cases either.

The only cases where CPU will access GDT for DS is if there's an explicit load of DS (a mov , lds or pop ds ), or a hardware task switch (eg from using a task gate). For "interrupt handler returning to virtual8086 mode" (where CPU does restore values from DS) the GDT isn't used because CPU is loading real mode compatible values. For CS and SS (but not DS) CPU will also use GDT when using interrupt/trap gates or call gates and returning (via. iret or retf ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM