Page 1 of 1

Do structure pad bytes ?

Posted: Sat Dec 27, 2003 12:00 am
by Gandalf
hi,

I have run into a bit of trouble. we have our page directory & table as a structure (so it contains bit fields like " char p:1 "). After loading our directory and tables we tried to load the cr3 and tried setting the pg bit in cr0 ( guess what ? our OS did a triple fault). but when we checked the values by printing them it was alright. Could it be because of padding of bits?

regards
Gandalf

RE:Do structure pad bytes ?

Posted: Sat Dec 27, 2003 12:00 am
by carbonBased
It's certainly possible, I would imagine.

Personally, I try to stay away from bit fields as much as possible when alignment is an issue.  I don't have much of a reason to, expect that I'm unsure as to whether there's a standard (ANSI) method for handling bit fields and alignment.

The following structures (for IDT, GDT and/or LDT) have worked well for me in a couple different OSs, however (and do, actually, use bit fields):

        struct _Descriptor {
                unsigned short limit_low;     /* limit 0..15    */
                unsigned short base_low;      /* base  0..15    */
                unsigned char base_med;       /* base  16..23   */
                unsigned char access;         /* access byte    */
                unsigned int limit_high:4;    /* limit 16..19   */
                unsigned int granularity:4;   /* granularity    */
                unsigned char base_high;      /* base 24..31    */
        } PACKED;

        struct _Gate {
                unsigned short offset_low;   /* offset 0..15    */
                unsigned short selector;     /* selector        */
                unsigned short access;       /* access flags    */
                unsigned short offset_high;  /* offset 16..31   */
        } PACKED;

Where PACKED (in GCC) is defined as:
__attribute__ ((packed))

Instead of using bit fields, I would recommend defining constants for each possible value, wherever possible:

        // Types of descriptors
        #define D_LDT          0x200   /* LDT segment                      */
        #define D_TASK         0x500   /* Task gate                        */
        #define D_TSS          0x900   /* TSS                              */
        #define D_CALL         0x0C00  /* 386 call gate                    */
        #define D_INT          0x0E00  /* 386 interrupt gate               */
        #define D_TRAP         0x0F00  /* 386 trap gate                    */
        #define D_DATA         0x1000  /* Data segment                     */
        #define D_CODE         0x1800  /* Code segment                     */

        // attributes for descriptors
        #define D_DPL3         0x6000  /* DPL3 or mask for DPL             */
        #define D_DPL2         0x4000  /* DPL2 or mask for DPL             */
        #define D_DPL1         0x2000  /* DPL1 or mask for DPL             */
        #define D_PRESENT      0x8000  /* Present (included by default)    */
        #define D_NOT_PRESENT  0x8000  /* Not Present                      */

        // more attributes for segment descriptors (not gates)
        #define D_ACC          0x100   /* Accessed (Data or Code)          */
        #define D_WRITE        0x200   /* Writable (Data segments only)    */
        #define D_READ         0x200   /* Readable (Code segments only)    */
        #define D_BUSY         0x200   /* Busy (TSS only)                  */
        #define D_EXDOWN       0x400   /* Expand down (Data segments only) */
        #define D_CONFORM      0x400   /* Conforming (Code segments only)  */
        #define D_BIG          0x40    /* Default to 32 bit mode           */
        #define D_BIG_LIM      0x80    /* Limit is in 4K units             */

Cheers,
Jeff

RE:Do structure pad bytes ?

Posted: Mon Jan 12, 2004 12:00 am
by dumbass101
Sounds like your "pad" bites
I didn't think guys needed them, though
I hope i'm not too helpful