Page 1 of 1

What entries should i have in my Global Descriptor Table?

Posted: Sun Jan 04, 2009 5:42 pm
by cjhawley001
I currently have these entries in my table:
1. Null
2. code segment
3. data segment

What other entries should be in my table? A paper i read from Intel said most modern Operating Systems have 6 entries.

Here is my GDT code if you are curious as to how i have it set up. (i also have my pointer in my header file)

Code: Select all

#include <system.h>
#include <gdt.h>
extern void gdt_flush();

gdt_entry_struct gdt[6];
gdt_ptr_struct gp;

void gdt_set(unsigned int index, unsigned long base, unsigned long limit, unsigned char access, unsigned char granularity)
{
	setBase(index, base);
	gdt[index].limit_low   = limit & 0xFFFF;
	gdt[index].granularity = granularity;
	gdt[index].access      = access;
}

void gdt_install()
{
   	// Setup the GDT pointer and limit 
	gp.limit = (sizeof(gdt_entry_struct) * 6) - 1;
	gp.base  = (unsigned int)&gdt;

   	gdt_set(0, 0, 0, 0, 0);
   	gdt_set(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
   	gdt_set(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
	//thanks to Bran's Kernel Development for these access and granularity values!	

   	//Flush out the old GDT and install the new changes!
   	gdt_flush();
}

void setBase(unsigned int index, unsigned long base)
{
	gdt[index].base_low    = base;
	gdt[index].base_middle = base >> BASE_MIDDLE_SHIFT;
	gdt[index].base_high   = base >> BASE_HIGH_SHIFT;
}
Thanks,

Chris

Re: What entries should i have in my Global Descriptor Table?

Posted: Sun Jan 04, 2009 6:49 pm
by JohnnyTheDon
It depends. If you are going to have user mode (PL=3) programs, you need at least a user mode code segment and a user mode data segment. The 6 entries you read about probably refer to long mode OSes, which need user segments for 32-bit and 64-bit operation. Another thing to consider when writing your GDT is SYSENTER/SYSLEAVE fast system call instructions and their AMD counterparts, SYSCALL/SYSRET. These instructions require your GDT to be structured in a specific way; look at the Intel manuals (I think they're in 2B) for info on them.

Re: What entries should i have in my Global Descriptor Table?

Posted: Sun Jan 04, 2009 7:11 pm
by Love4Boobies
This is on the wiki, on tens of other threads and in the Intel manuals. RTFM!

Re: What entries should i have in my Global Descriptor Table?

Posted: Sun Jan 04, 2009 8:00 pm
by cjhawley001
I apologize, but from what i see on the wiki it only lists 2 entries, and the null entry.

As for the manuals, I have gone through them, and other articles, which have all led me to the 6 entries for the table.

If I do not know as much on development as you do, I apologize, as frankly I am new to it. I have tried it in the past and failed, so I took a break to understand theory.

I asked a question, and there was no need for "RTFM!".

Thanks,
Chris

Re: What entries should i have in my Global Descriptor Table?

Posted: Sun Jan 04, 2009 8:09 pm
by Brendan
Hi,

For most OS's, 6 GDT entries probably works out to:
  • NULL entry
  • CPL=0 code segment
  • CPL=0 stack/data segment
  • CPL=3 code segment
  • CPL=3 stack/data segment
  • System TSS (needed for CPL=0 -> CPL=3 -> CPL=0 context switches, not for task switches)
Extra entries might (optionally) be used for a kernel API call gate, and because GDT entries need to be in a different order for SYSENTER and SYSCALL (if an OS supports both then you can expect a few redundant entries).


Cheers,

Brendan

Re: What entries should i have in my Global Descriptor Table?

Posted: Sun Jan 04, 2009 10:59 pm
by System123
It also depends if you want to be able to load 16 and 32 bit programs later. I have the following entries in my GDT.
  • NULL Descriptor
  • Kernel code segment 32bit
  • Kernel data segment 32bit
  • User code segment 32bit
  • User data segment 32bit
  • System TSS
  • Kernel code segment 16bit
  • Kernel data segment 16bit