Page 1 of 1

IDT using bitfields

Posted: Tue Apr 13, 2004 11:00 pm
by Gnome
Hey everyone.

I'm working on cleaning up my interrupt code by using bitfields for the IDT structures, but I'm getting a GPF sometime after I load the descriptor table (it usually manifests itself on function calls or jumps, it seems, but it's erratic -- it might be at some time small time interval after loading the IDT), so I figure my definitions are wrong. Here's what I have now, which is matched as closely as I can figure to what it should be:

struct idt_desc_t
{
        unsigned high_offset : 16;
        unsigned present     : 1;
        unsigned priv_level  : 2;
        unsigned reserved0   : 5;
        unsigned reserved1   : 3;
        unsigned             : 5;
        unsigned selector    : 16;
        unsigned low_offset  : 16;
};

As a side note, I'm curious as to what the reserved fields are. Every example I've seen just says something to the effect of, "they should always be this value".

RE:IDT using bitfields

Posted: Wed Apr 14, 2004 11:00 pm
by common
Well, if I were you, I would simply avoid bitfields.  However, if you are using gcc, you may want to specify __attribute__ ((packed)) at the end of your structural definition.

As for the reserved types, that's primarily intended to be used (possibly) for later use.  They state that it should be set to X value so they can work around that value in later implementation without affecting your current code's operations if it doesn't support that specific, new feature.

RE:IDT using bitfields

Posted: Wed Apr 14, 2004 11:00 pm
by common
Also, didn't realize this until after I sent the last message...your structure may need to be inverted there...

struct idt_desc_t
{
unsigned low_offset: 16;
unsigned selector : 16;
...

RE:IDT using bitfields

Posted: Sat Apr 17, 2004 11:00 pm
by Gnome
You're right... I didn't notice that, and it's been fixed, but it still doesn't work.

I used the example on part 3 of the interrupts tutorial at www.osedever.net (it's down now for some reason), setting the fields as they did, and using a hexdump to match it up. The struct is now correct, but it still throws a GPF (which triple faults). Bochs says that the gate descriptor is not a valid sys reg.

RE:IDT using bitfields

Posted: Sat Apr 17, 2004 11:00 pm
by common
struct idt_desc_t
{
unsigned low_offset : 16;    /* low word of address of handler */
unsigned code_segment : 16;  /* should select the appropriate entry in GDT */
unsigned reserved0 : 5;      /* set to all zeros */
unsigned all_zero0 : 3;      /* set to all zeros */
unsigned indicator : 5;      /* 0xE = 32bit interrupt gate, 0xF = 32bit trap */
unsigned dpl : 2;            /* descriptor privilege level (probably 0) */
unsigned present : 1;        /* must be set to 1 */
unsigned high_offset : 16;   /* high word of address of handler */
};

I believe that is correct.

RE:IDT using bitfields

Posted: Tue Apr 20, 2004 11:00 pm
by Anton
Probobly you need to use "unsigned long long" instead of "unsigned". ?
:)
Atnon.

RE:IDT using bitfields

Posted: Tue Apr 20, 2004 11:00 pm
by common
And why would you need to do that?

RE:IDT using bitfields

Posted: Wed Apr 21, 2004 11:00 pm
by Anton
There are several reasons(not serious, but how knows).
1)I picture the desrcipter as a 64-bit integer, not as a bunch of 32-bit word or 16-bit words.
2)So that it will be easier( at one glance) to figure out the size of the struct.
3)To be shure there will be no overlaping problems.
Anton.

RE:IDT using bitfields

Posted: Wed Apr 21, 2004 11:00 pm
by common
My response to that :)

1)  But it is not a single 64-bit integer, after all, it is a structure, or, more appropriately, a type of union.

2)  unsigned long long doesn't have to be 64-bit, all C99 says is that it must have a greater sizeof than unsigned long, which, in turn, must be greater than an unsigned short, which, in turn, must be greater than a char.  Also, this is why you have comments.  Never try to use data types as briefs for what is going on, only the name should be used to indicate such a relationship (i.e. usage of typedef).  In general, however, and again, comments, comments, comments.

3)  Be sure there are no overlapping problems?  unsigned long long might actually cause a non-packed alignment, depending on the compiler, though bitfields in any structure can be dangerous about this, regardless of data type.