Using C code in segmented mode, question

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.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Using C code in segmented mode, question

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Using C code in segmented mode, question

Post 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.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Using C code in segmented mode, question

Post 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)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Using C code in segmented mode, question

Post 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.
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Using C code in segmented mode, question

Post 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!
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Using C code in segmented mode, question

Post by mangaluve »

I got confused since they use C code and segmentation in the tutorial I linked...
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Using C code in segmented mode, question

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: Using C code in segmented mode, question

Post 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?
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Using C code in segmented mode, question

Post 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...?
Every good solution is obvious once you've found it.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: Using C code in segmented mode, question

Post by AlfaOmega08 »

Version 2.6.27.17. You can take a copy here:
http://www.kernel.org/pub/linux/kernel/ ... 17.tar.bz2
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Using C code in segmented mode, question

Post 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?
Every good solution is obvious once you've found it.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: Using C code in segmented mode, question

Post 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?
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Re: Using C code in segmented mode, question

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Using C code in segmented mode, question

Post 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 :D

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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Using C code in segmented mode, question

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply