Page 1 of 2
Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 6:58 am
by mangaluve
I've just started with OS development and I'm currently learning about protected mode and segmented memory. Im looking at the tutorial
http://www.osdever.net/tutorials/brunma ... ial_03.php. When it comes to memory addressing in segmented mode, I know how it works in assembler, for instance by using the address 08h:0002h (segment:offset). But how does it work when I use C-code aswell? When I use an address in C (for instance, I create a pointer to a location), how do I tell the compiler which segment it is and so on? Im getting so confused about this.
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 7:14 am
by JamesM
Hi,
The simple answer is - it doesn't. All compilers* assume a flat memory model, and ignore segment registers. This works fine in protected mode, but you're going to get into real difficulties in real mode.
Cheers,
James
* With the exception of one? I remember it being mentioned but can't recall it's name. I'm sure someone will enlighten.
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 7:19 am
by mangaluve
Thanks, but I dont really understand. How can it work in protected mode? Because I am in protected mode, using segmentation. So the memory model is not flat. How can that work with the compiler? How can the compiler even produce C-code that writes to a certain location, when segmentation is used? Doesnt I have to using segment:offset Allways (in protected mode with segmentation on)
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 7:44 am
by Solar
mangaluve wrote:How can it work in protected mode? Because I am in protected mode, using segmentation. So the memory model is not flat.
Then you cannot use a C compiler, as they assume a flat memory model - simply because there is no way to distinguish segments in C code without breaking the language standard.
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 7:45 am
by JamesM
mangaluve wrote:Thanks, but I dont really understand. How can it work in protected mode? Because I am in protected mode, using segmentation. So the memory model is not flat. How can that work with the compiler? How can the compiler even produce C-code that writes to a certain location, when segmentation is used? Doesnt I have to using segment:offset Allways (in protected mode with segmentation on)
The compiler assumes you're using a flat memory model. It assumes that DS == SS; and that all data can be accessed via each. You can't change these assumptions - if you decide to use segmentation you will have to pay the consequences and not use a compiler (to its full ability).
EDIT: Touche Solar!
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 7:55 am
by mangaluve
I got confused since they use C code and segmentation in the tutorial I linked...
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 8:53 am
by xenos
I think there is a way using segmentation and far pointers in Watcom C compiler... Maybe you find some hints in the
Watcom manuals. But I haven't used that for a while. I switched to GCC using only flat memory.
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 10:13 am
by AlfaOmega08
In linux source code, there are some files which seems to work in real mode. Look at arch/x86/boot/* files. a20.c uses real mode interrupts. How do this work?
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 10:21 am
by Solar
Erm... which version of the Linux sources are you talking about? I don't have a "x86" folder here, and "a20.c" is nowhere to be seen...?
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 10:23 am
by AlfaOmega08
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 10:24 am
by Solar
Hm. Strange. Why isn't that file on my machine?
OK, nevermind. All I see there is set_fs() and set_gs()... do you see anything I don't?
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 10:35 am
by AlfaOmega08
On line 83 there is a call to the BIOS Interrupt 15.
ok... maybe I misunderstood. GCC can handle real mode code but you have to pay attentio to not use pointers. Right?
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 7:49 pm
by iammisc
GCC can be used in real mode if none of your code happens to never compile into assembler that doesn't assume the flat memory model assumptions made by gcc. Seeing how this is quite difficult to determine for sure, the easiest answer is just no.
Re: Using C code in segmented mode, question
Posted: Mon Feb 23, 2009 10:03 pm
by neon
JamesM wrote:* With the exception of one? I remember it being mentioned but can't recall it's name. I'm sure someone will enlighten.
Turbo C FTW
Its a great 16 bit compiler, btw
Not sure of its use in OS development though.
When it comes to memory addressing in segmented mode, I know how it works in assembler, for instance by using the address 08h:0002h (segment:offset). But how does it work when I use C-code aswell?
far and near pointers. near pointers are just 16 bit offsets from the current data segment, and far pointers are 32 bit linear addresses. 16 bit compiliers useually have a method of setting the segments and making far pointers from segment/offset pairs.
ie, Turbo C's MK_FP(seg,off) makes a 32 bit far pointer from a seg:offset pair.
Re: Using C code in segmented mode, question
Posted: Tue Feb 24, 2009 2:34 am
by xenos
neon wrote:far and near pointers. near pointers are just 16 bit offsets from the current data segment, and far pointers are 32 bit linear addresses.
Far pointers are not linear addresses! One has to distinguish between four different types of pointers:
16 bit adressing mode:
near pointer = 16 bit address relative to current data segment (stored in ds)
far pointer = 16 bit segment selector + 16 bit address relative to segment given by the segment selector
32 bit adressing mode:
near pointer = 32 bit address relative to current data segment (stored in ds)
far pointer = 16 bit segment selector + 32 bit address relative to segment given by the segment selector
The
linear address is the sum of the segment base and the offset, i.e. the address relative to the segment. In real mode, only 16 bit addressing mode is supported and the base address of any segment seg is given by (seg << 4), i.e. a segment selector value of 0xf000 represents a segment with base 0xf0000. In protected mode, the segment selector is a pointer into a table with base addresses, segment lengths, access rights... Both 16 bit and 32 bit addressing modes are supported, depending on the address size parameter of the current code segment. Typically, modern C compilers generate code assuming 32 bit addressing mode*, use only near pointers and assume that all segment selectors have a base value of 0.
* Except when you tell them to generate 16 bit code.