What entries should i have in my Global Descriptor Table?

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
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

What entries should i have in my Global Descriptor Table?

Post 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
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

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

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post by Love4Boobies »

This is on the wiki, on tens of other threads and in the Intel manuals. RTFM!
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

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

Post 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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

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

Post 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
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Post Reply