gdt

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.
Post Reply
_guest

gdt

Post 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
Whatever5k

Re:gdt

Post 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.
Tim

Re:gdt

Post 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)
slacker

Re:gdt

Post 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....
shad

Re:gdt

Post 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..
slacker

Re:gdt

Post 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
Tim

Re:gdt

Post 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.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:gdt

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
slacker

Re:gdt

Post by slacker »

beyond: i like using compilers....just not for setting up system architecture,,like gdt, idt, etc....
Post Reply