Page 1 of 1

gdt

Posted: Sat Apr 26, 2003 11:22 pm
by _guest
i have been searching so much on the net for an OS which has its GDT written in C. but i couldnt find anything. if anyone has written it plz show it to me
thanx for ur help

Re:gdt

Posted: Sun Apr 27, 2003 4:42 am
by Whatever5k
Uh? There are thousands of OSes out there using C to setup a GDT (alright, maybe not thousand ;)) - have a look at TheMoebius, Minix or something else.

Re:gdt

Posted: Sun Apr 27, 2003 4:43 am
by Tim
I wrote my GDT setup code in C. It's pretty easy. Just declare a descriptor struct, and your GDT is an array of these. You'll also need a GDTR struct. Remember to turn on packing on all these structures.

Code: Select all

#pragma pack (push, 1)  /* align structures to a byte boundary */

/* Segment desciptor definition */
struct descriptor_t
{
   uint16_t limit;
   uint16_t base_l;
   uint8_t base_m;
   uint8_t access;
   uint8_t attribs;
   uint8_t base_h;
};

typedef struct descriptor_t descriptor_t;

struct gdtr_t
{
   uint16_t limit;
   uint32_t base;
};

typedef struct gdtr_t gdtr_t;

#pragma pack(pop)

Re:gdt

Posted: Sun Apr 27, 2003 10:40 am
by slacker
i like to do everything that is written about in the INTEL manuals in pure ASM code like GDT, IDT, etc. other compilers will work but i find it easier to debug problems since ur working in pure ASM. i think compilers should only be used when your Code doesnt deal with actually setting up your system architecture because your never sure of every line of code it generates....i guess i dont trust compilers....

Re:gdt

Posted: Sun Apr 27, 2003 10:47 am
by shad
What is this packing thing? My structures were getting loaded wrong so i made an asm function to load the correct base in the gdtr.. but it seems this #pragma thing could help me avoid that..

Re:gdt

Posted: Sun Apr 27, 2003 11:06 am
by slacker
shad your making it too complicated...its 2x easier if you write it in ASM:

;---constants---
GDTpointer:
GDTlim DW ENDGDT-GDT-1
GDTbase DD 0x500 ;we will be moving GDT to 0x50:0x0

GDT_Selectors: ;used as offset from start of GDT
NULLsel EQU 0x0
CODEsel EQU 0x0008
DATAsel EQU 0x0010

;---gdt---
GDT: ;flat memory model
;NULLsel ;required
DD 0x00
DD 0x00

;CODEsel
DW 0xFFFF
DW 0x00
DB 0x00
DB 0x9A
DB 0xCF
DB 0x00

;DATAsel
DW 0xFFFF
DW 0x00
DB 0x00
DB 0x92
DB 0xCF
DB 0x00
ENDGDT:




there--your done. now work on your IDT--its trickier

Re:gdt

Posted: Sun Apr 27, 2003 11:27 am
by Tim
i guess i dont trust compilers....
No, I think you just don't understand them.
What is this packing thing?
By default, the compiler aligns structure fields on multiples of their size to make things faster. To do this, it adds bytes in between fields as needed.

If you declare this:

Code: Select all

struct gdtr_t
{
    uint16_t limit;
    uint32_t base;
};
then what the compiler really sees is:

Code: Select all

struct gdtr_t
{
    uint16_t limit;
    uint16_t PADDING;
    uint32_t base;
};
Here, the compiler has added an extra two bytes in the middle to make sure that the base member is aligned on a 4-byte boundary.

Normally this doesn't make any difference, but it does when you need to pass the structures to the hardware. So you need to turn off alignment. In gcc, you can either use a #pragma like I did here, or add __attribute__((packed)) at the end.

Re:gdt

Posted: Mon Apr 28, 2003 1:16 am
by distantvoices
slacker: better trust your compiler *gg* It does a lot of work for you the assembler (even nasm) won't, and the people working on compiler stuff do hard and good work.

I trust my compiler. I see the result and the result is a running program just as I expect it. So why not trrusting it? When it says there is an error, it is clear to me, that I have to do debugging. It canna tell all errors - see your string/pointer problem, slacker - but most syntactic and some semantic ones. you canna expact a compiler to recoginze EVERYTHING.

Simply put: Get used to your compiler. It will be a good friend.

Re:gdt

Posted: Mon Apr 28, 2003 12:58 pm
by slacker
beyond: i like using compilers....just not for setting up system architecture,,like gdt, idt, etc....