GDT Tutorial: Difference between revisions
Jump to navigation
Jump to search
[unchecked revision] | [unchecked revision] |
Content deleted Content added
No edit summary |
Fixed bit shifting error as pointed out on the talk page, some comments and changed SEG_TYPE to SEG_DESCTYPE to differentiate segment and descriptor types |
||
Line 214: | Line 214: | ||
// Each define here is for a specific flag in the descriptor. |
// Each define here is for a specific flag in the descriptor. |
||
// Refer to the intel documentation for a description of what each one does. |
// Refer to the intel documentation for a description of what each one does. |
||
#define |
#define SEG_DESCTYPE(x) ((x) << 0x04) // Descriptor type (0 for system, 1 for code/data) |
||
#define SEG_PRES(x) ((x) << 0x07) // Present |
#define SEG_PRES(x) ((x) << 0x07) // Present |
||
#define SEG_SAVL(x) ((x) << 0x0C) // Available for system use |
#define SEG_SAVL(x) ((x) << 0x0C) // Available for system use |
||
#define SEG_LONG(x) ((x) << 0x0D) // Long mode |
#define SEG_LONG(x) ((x) << 0x0D) // Long mode |
||
#define SEG_SIZE(x) ((x) << 0x0E) // Size (0 for 16-bit, 1 for 32) |
#define SEG_SIZE(x) ((x) << 0x0E) // Size (0 for 16-bit, 1 for 32) |
||
#define SEG_GRAN(x) ((x) << 0x0F) // Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB) |
#define SEG_GRAN(x) ((x) << 0x0F) // Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB) |
||
#define SEG_PRIV(x) (((x) & 0x03) << 0x05) // Set privilege level (0 - 3) |
#define SEG_PRIV(x) (((x) & 0x03) << 0x05) // Set privilege level (0 - 3) |
||
#define SEG_DATA_RD 0x00 // Read-Only |
#define SEG_DATA_RD 0x00 // Read-Only |
||
Line 239: | Line 239: | ||
#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed |
#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed |
||
#define GDT_CODE_PL0 |
#define GDT_CODE_PL0 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ |
||
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
||
SEG_PRIV(0) | SEG_CODE_EXRD |
SEG_PRIV(0) | SEG_CODE_EXRD |
||
#define GDT_DATA_PL0 |
#define GDT_DATA_PL0 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ |
||
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
||
SEG_PRIV(0) | SEG_DATA_RDWR |
SEG_PRIV(0) | SEG_DATA_RDWR |
||
#define GDT_CODE_PL3 |
#define GDT_CODE_PL3 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ |
||
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
||
SEG_PRIV(3) | SEG_CODE_EXRD |
SEG_PRIV(3) | SEG_CODE_EXRD |
||
#define GDT_DATA_PL3 |
#define GDT_DATA_PL3 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ |
||
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ |
||
SEG_PRIV(3) | SEG_DATA_RDWR |
SEG_PRIV(3) | SEG_DATA_RDWR |
||
void |
void |
||
Line 261: | Line 261: | ||
// Create the high 32 bit segment |
// Create the high 32 bit segment |
||
descriptor = limit & 0x000F0000; |
descriptor = limit & 0x000F0000; // set limit bits 19:16 |
||
descriptor |= (flag << 8) & 0x00F0FF00; |
descriptor |= (flag << 8) & 0x00F0FF00; // set type, p, dpl, s, g, d/b, l and avl fields |
||
descriptor |= (base >> 16) & |
descriptor |= (base >> 16) & 0x000000FF; // set base bits 23:16 |
||
descriptor |= base & 0xFF000000; // set base bits 31:24 |
|||
// Shift by 32 to allow for low part of segment |
// Shift by 32 to allow for low part of segment |
||
Line 269: | Line 270: | ||
// Create the low 32 bit segment |
// Create the low 32 bit segment |
||
descriptor |= base << 16; |
descriptor |= base << 16; // set base bits 15:0 |
||
descriptor |= limit & 0x0000FFFF; |
descriptor |= limit & 0x0000FFFF; // set limit bits 15:0 |
||
printf("0x%.16llX\n", descriptor); |
printf("0x%.16llX\n", descriptor); |