Page 1 of 1
Segments
Posted: Mon Jul 05, 2004 11:00 pm
by Mange
I got a question. Since Im in PM using segments,I can access memory with
Descriptor:Offset in ASM. But how do I do this in my kernel in C?
Can I still use the physical adresse? Like for instace I got a segment (08h), with base adresse 0x100, and I wanna reach 08h:0 ... instead of writing this way, can I use 0x100? Or do I have to tell which descriptor to use?
RE:Segments
Posted: Tue Jul 06, 2004 11:00 pm
by Anton
You can't choose which segment to use in a C program. You need to load the correct selectors before calling your C code.(or use inline asm) Once the correct segment is loaded, yes, you can access it by 0x100.
RE:Segments
Posted: Tue Jul 06, 2004 11:00 pm
by Mange
So I can still use the physical adress? No matter how my segments look?
RE:Segments
Posted: Tue Jul 06, 2004 11:00 pm
by GT
No. All C pointers are "near" pointers, using segments implicitly, assuming we're talking about GCC here. And, for that matter, if you're in protected mode, all memory references in any language, C, Assembly, whatever, use the appropriate segment registers. The only way to "just use the physical address" is to load your data segment with a base of 0 and a limit of 4GB (you're still using logical addresses, but in that case the logical address and the physical address are identical).
RE:Segments
Posted: Tue Jul 06, 2004 11:00 pm
by Mange
But why do I need a code segment and a data segment? Whats the difference between them?
RE:Segments
Posted: Tue Jul 06, 2004 11:00 pm
by Mange
But why do I need a code segment and a data segment? Whats the difference between them?
RE:Segments
Posted: Tue Jul 06, 2004 11:00 pm
by GT
"But why do I need a code segment and a data segment?"
Because that's how the chip designers set it up. There are times when it's nice to be able to isolate code from data, so being able to set these up seperately is a feature. Being forced to set them up, even if you're not using this feature, is a pain, but it simplified the chip design. Just set them up with the same base and limit, and it works more or less like you'd expect. Set all of them with a base of 0 and a limit of 4GB and from that point forward you can pretend they don't exist (except for purposes of switching privelege levels).
"Whats the difference between them?"
There are different features you can set in their attribute bits (e.g. you can only specify code segments as conforming or not, you can only specify data segments as writable or not, etc.)