Page 1 of 1
GDT base and limit values questions (how and why)?
Posted: Sun Jul 17, 2005 4:11 pm
by calpis
Hi, I wonder if anyone could tell me why the GDT base value limit for code segment and data segment are 0 and FFFFFFFF? Why do we overlap the values for both segments(hope is not too silly to ask)? And when do we not overlap it.(or what if it don't overlap). Same goes on the user level code and data segment.
And what happen if I assign other base and limit value to supervisor/user level segemnts. I just want to understand how/when/why do we use it.
Thanks in advance.
Re:GDT base and limit values questions (how and why)?
Posted: Sun Jul 17, 2005 4:56 pm
by AR
The segments cover each other so that both the code and data are in the same address space. If the base is different then the data will be at a different location then where it is physically.
If we have paging off then we have CodeBase=0x100000 DataBase=0x200000, Then movl %ds:(0), %eax will access 0x200000 physical. But if EIP=0 then EIP will be at 0x100000 Physical. The obvious problem is that you can't read the code and it generally messes with your head just like realmode except that it is more difficult to change the Base.
Paging is more configurable, it will be a lot simpler just to use that, even more so because no compiler is capable of producing code that will work in segmentation without overlapping segments unless you feel like modifying LD or that you are going to require all programs be written in assembly.
If you want DEP then you can try modifying the limit so that the segments still overlap but only up to where the code finishes to prevent execution in the stack or heap, of course the obvious problem is that Limit is not very granular and even if you do get it to work then JIT compilers won't work (Stuff like .Net, Java, etc). Newer CPUs have DEP support when PAE paging is enabled anyway.
Re:GDT base and limit values questions (how and why)?
Posted: Thu Jul 21, 2005 1:02 pm
by calpis
Thanks. OK. I think I get it...basically.
Re:GDT base and limit values questions (how and why)?
Posted: Fri Jul 22, 2005 5:50 am
by Freanan
If i got your problem right...
the code and data segment are often overlapped in example code to provide some kind of flat adress space. But this is only a design choice and you can also seperate the segments.
Re:GDT base and limit values questions (how and why)?
Posted: Fri Jul 22, 2005 5:55 am
by Solar
Add to that that virtually no development tools properly support anything else but a flat memory model... you would end up having to write your own compiler.
Segments could have avoided several of the problems we had these years (several categories of overflow exploits, for example), if properly applied. But they are a pain to apply properly, so virtually everyone has ignored them from the day we got proper 32bit addressing and paging.
Seeing how they are now phased out in the AMD64, I wouldn't bother too much with them.
Re:GDT base and limit values questions (how and why)?
Posted: Fri Jul 22, 2005 6:17 am
by calpis
Thanks all. OK I get that now. we do that for flat mem model.
OK, What about there values for user level and supervisor level? I mean, can I assign supervisor code & data for the base and limit 0 - FFFFFFFF
and
User code & data for the base and limit say...C0000000 - FFFFFFFF or something? hence all four segments are flat mem model right? What effect will this be to the whole kernel if i use don't assign 00000000 -FFFFFFFF to user code & data segment?
Also is user level & kernel segments represent so called user space and kernel space in kernel design on the book? I don't know if it will be related to kernel design...its really confusing or this is just another stupid question.
Thanks again.
Re:GDT base and limit values questions (how and why)?
Posted: Fri Jul 22, 2005 7:00 am
by AR
A flat memory model involves 0 - FFFFFFFF for both code and data for both user space and kernel space. Changing the Base creates a non-linear address space which is just plain confusing although it may be useful to make a user program think it is loaded at 0 when the kernel has reserved the lower 1GB instead of the high 1GB - of course that's assuming you won't ever support AMD64 where segmentation is linear, period.
Kernel segments are for the kernel, User Segments are for user space, hence the names... (Supervisor and non-Supervisor pages also mean kernel or user space respectively)