Do I need the GDTPTR after loading it with LGDT

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.
Post Reply
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Do I need the GDTPTR after loading it with LGDT

Post by teodori »

Hello, say I put my GDT with 256 descriptor from 0x1000 to 0x2000, then load with LGDT my GDTPTR made of 10 bytes from any location.
Is it then possible to overwrite the memory of my GDTPTR?
I know that the GDT must not be overwritten, but what about my GDTPTR containing the virtual address and the size of the GDT!
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Re: Do I need the GDTPTR after loading it with LGDT

Post by thomasloven »

Testing...
My kernel won't boot...
Without actually reading the documentation again, I believe this would make sense. The GDT register is probably 32 bits, while the gdt pointer is 40 bits iirc.

But if you're short on space, you can store the gdt pointer in gdt entry 0 (which is unused). Works on qemu and bochs at least.
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: Do I need the GDTPTR after loading it with LGDT

Post by teodori »

Hm I think we missunderstand us. Here is a part of my code.
I copy the the GDT from my .rodata to memory, but my GDTPTR is loaded with LGDT and it remains in the .rodata, which part will be delete when the kernel loader exits.

Code: Select all

...
gdt64:
	# Null Descriptor
	.word 0x0000, 0x0000
	.byte 0x00, 0b00000000, 0b00000000, 0x00
	# Code Descriptor
	.word 0xffff, 0x0000
	.byte 0x00, 0b10011010, 0b10101111, 0x00
	# Data Descriptor
	.word 0xffff, 0x0000
	.byte 0x00, 0b10010010, 0b10101111, 0x00
gdt64_end:

gdt64ptr:
	.word (gdt64_end - gdt64 - 1)
	.quad 0x0000
...
	movl $(INITSEG * 0x10 + gdt64), %esi
	movl $0x0000, %edi
	movl $(gdt64_end - gdt64), %ecx
	rep movsb
...
	lgdt (INITSEG * 0x10 + gdt64ptr)
...
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Re: Do I need the GDTPTR after loading it with LGDT

Post by thomasloven »

Oh. I assumed you were talking about a 32 bit kernel.
I have no experience with 64 bit programming. Sorry.
doxrobot
Member
Member
Posts: 30
Joined: Wed May 15, 2013 10:14 am

Re: Do I need the GDTPTR after loading it with LGDT

Post by doxrobot »

yes it's fine. you can over-write it.
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: Do I need the GDTPTR after loading it with LGDT

Post by teodori »

thank you :)
AbstractYouShudNow
Member
Member
Posts: 92
Joined: Tue Aug 14, 2012 8:51 am

Re: Do I need the GDTPTR after loading it with LGDT

Post by AbstractYouShudNow »

Why would you want to overwrite it ?
Basically, a C kernel defines a struct gdtr, takes a static variable of that type, and use lgdt with a pointer to the structure, so you don't need to overwrite it at least.

Anyways, I don't know whether ALL x86 compatible processors will cache the value in memory so that you can overwrite gdtr. This would require extensive testing, but would remain an ugly hack though
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Do I need the GDTPTR after loading it with LGDT

Post by Brendan »

Hi,
AbstractYouShudNow wrote:Why would you want to overwrite it ?
I'd assume the idea is to free RAM that was used during boot so it's not left allocated for no reason forever. The GDTPTR is small, but if you've got code to free a "no longer needed after boot" area for other reasons then it makes sense to put whatever you can (including small things) in that area.
AbstractYouShudNow wrote:Anyways, I don't know whether ALL x86 compatible processors will cache the value in memory so that you can overwrite gdtr. This would require extensive testing, but would remain an ugly hack though
Intel CPUs have done this since the beginning (80386), and I doubt any CPU manufacturer would want to be "less compatible" without a very good reason.

If a CPU didn't keep track of the address of the GDT it'd have to keep track of the address of the GDTPTR instead (it'd be just as much work for CPU designers), and every time the CPU needs something from the GDT it'd have an extra fetch to access the GDTPTR first (it wouldn't be faster). Basically, there isn't a good reason for a CPU manufacturer to want to be "less compatible", and I'd be happy to assume all 80x86 CPUs keep track of the address of the GDT.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply