Question about 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
User avatar
samoz
Member
Member
Posts: 59
Joined: Sun Jun 01, 2008 1:16 pm

Question about GDT

Post 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?
Hexciting: An open source hex editor for the command line.
https://sourceforge.net/projects/hexciting/
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Question about GDT

Post 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).
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Question about GDT

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