Page 1 of 1

Question about GDT

Posted: Tue Sep 30, 2008 5:43 pm
by samoz
Hey guys, I'm working on my operating system, using the James Molloy tutorials as a guide, but mainly trying to just use the Intel manuals.

I'm reading the section where he sets up the GDT with the following code:

Code: Select all

static void gdt_set_gate(s32int num, u32int base, u32int limit, u8int access, u8int gran)
{
   gdt_entries[num].base_low    = (base & 0xFFFF);
   gdt_entries[num].base_middle = (base >> 16) & 0xFF;
   gdt_entries[num].base_high   = (base >> 24) & 0xFF;

   gdt_entries[num].limit_low   = (limit & 0xFFFF);
   gdt_entries[num].granularity = (limit >> 16) & 0x0F;

   gdt_entries[num].granularity |= gran & 0xF0;
   gdt_entries[num].access      = access;
} 
I see where he sets the first part of the limit with the limit_low field, but what about the other 4 bits? Are those set with the granularity byte? This would make sense, since the gran byte is 8 bits, but I just wanted to make sure.

P.S.
It'd be cleaner to use a 4 bit data type, but I don't think those exist in C?

Re: Question about GDT

Posted: Tue Sep 30, 2008 6:38 pm
by neon
The granularity byte has the format:

Bit 0-3 Represents bits 16-19 of the segment limit.
Bit 4 We can use this for whatever
Bit 5 Reserved
Bit 6 is the segment type (16 or 32 bit)
Bit 7 Granularity. By setting to 1, each segment will be bounded by 4KB.

His code sets the high 4 bits of limit to the low 4 bits of the granularity byte (Bits 0-3).
It'd be cleaner to use a 4 bit data type, but I don't think those exist in C?
Nope. Smallest data type is a byte (char).

Re: Question about GDT

Posted: Thu Oct 02, 2008 1:51 am
by AJ
neon wrote:
It'd be cleaner to use a 4 bit data type, but I don't think those exist in C?
Nope. Smallest data type is a byte (char).
How about bitfields?

Cheers,
Adam