How to read data in GDT using instruction sgdt?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Locked
leetow2003
Member
Member
Posts: 70
Joined: Fri Nov 19, 2010 6:54 pm

How to read data in GDT using instruction sgdt?

Post by leetow2003 »

I define a temporary data segment that saves GDTR,and then I
want to read the second descriptor data in GDT using instruction
sgdt,but when I want to get the data by base address in GDTR,
I don't know what data segment I must use,How to read data?
Look:

Code: Select all

;Temporary data segment
tseg  segment   use16
    db 100 dup(0)
tseg  ends

;In P-Mode
     mov ax,datat_sel  ;datat_sel is the temporary data segment selector
     mov ds,ax
     mov bx,0
     sgdt fword ptr [bx]  ;To save GDTR into temporary data segment 
     mov esi,[bx+2]  ;Save GDT base address into ESI
     ;Following instruction is error,the PC is always restart when it run,How to correct it?
     mov dh,[esi+7] ;I wand to read the second descriptor


User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to read data in GDT using instruction sgdt?

Post by Combuster »

The GDT itself is exempt from segmentation, so if nontrivial segmentation is actually in use, you'll have to find a way to determine what virtual address corresponds to the linear address returned by sgdt.

And even then, paging may still refuse access. If you're running in ring 3 or it's not your OS, the amount of data you can probably get is either not apparently useful or leads to a potential security breach. Which leads to the question: what do you want to achieve with the GDT contents, and for what reason do you think you need SGDT for that?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
leetow2003
Member
Member
Posts: 70
Joined: Fri Nov 19, 2010 6:54 pm

Re: How to read data in GDT using instruction sgdt?

Post by leetow2003 »

Combuster wrote:The GDT itself is exempt from segmentation, so if nontrivial segmentation is actually in use, you'll have to find a way to determine what virtual address corresponds to the linear address returned by sgdt.

And even then, paging may still refuse access. If you're running in ring 3 or it's not your OS, the amount of data you can probably get is either not apparently useful or leads to a potential security breach. Which leads to the question: what do you want to achieve with the GDT contents, and for what reason do you think you need SGDT for that?
In a segment description,its property bit 0 expresses
if the segment is accessed,so I want to read the value about accessed property,
Could you tell me how to do?
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: How to read data in GDT using instruction sgdt?

Post by Chandra »

SGDT expects a 48 bit operand. None of the 32 bit registers can hold this value. If you do use a 32 bit resister only LIMIT portion of the GDT is stored in the register. Hence, it is recommended to dynamically allocate memory and pass this address as an operand to the SGDT instruction. I use something like this in my debugging utility, works awesome.
As Combuster said, paging might be a trouble in some cases. This can be resolved by identity mapping the 1st megabyte of the memory and then copying the GDT to somewhere inside this memory range. At most, paging can be kept disabled until you complete this task.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to read data in GDT using instruction sgdt?

Post by Combuster »

leetow2003 wrote:I want to read the value about accessed property,
Could you tell me how to do?
Why did you spam a new thread when the real question is asked and answered in an old thread, created by yourself? You created the GDT, you installed the same GDT, so you should know the actual locations to read/write to get the necessary information. You do not need SGDT or any segmentation opcode for it.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Locked