User:Johnburger/Demo/x86/Seg

From OSDev Wiki
Jump to navigation Jump to search

At the risk of repeating myself, a Segment is defined by Intel to point into a Descriptor Table - either the GDT, LDT or IDT.

       1111110000000 0 00
       FEDCBA9876543 2 10
       ^^^^^^^^^^^^^ ^ ^^
       ||||||||||||| | `+-- Requested Privilege Level: (00-03)
       ||||||||||||| `----- Table Indicator:            0=GDT, 1=LDT
       `++++++++++++------- Actual selector into Indicated Descriptor Table

To make it easy on myself, I defined some constants, and some macros, that let me do the following:

             MOV     AX,Selector(Index,LDT,RPL3)

At least now I can write code that warns me if I forget a critical parameter - like the fact that the Index is in the Local Descriptor Table, rather than the Global one!

Demo/x86/Seg.inc

;
; x86/Seg.inc
;

; These are the definitions for the Segment selector registers

x86.Seg.Index   EQU            1111_1111_1111_1000b  ; Index into Table
x86.Seg.TI      EQU            0000_0000_0000_0100b  ; Table Indicator (GDT/LDT)
x86.Seg.RPL     EQU            0000_0000_0000_0011b  ; Requested Privilege Level

x86.Seg.LDT     EQU            x86.Seg.TI       ; Add this in to every LDT seg
x86.Seg.GDT     EQU            0

x86.Seg.RPL3    EQU            0000_0000_0000_0011b
x86.Seg.RPL2    EQU            0000_0000_0000_0010b
x86.Seg.RPL1    EQU            0000_0000_0000_0001b
x86.Seg.RPL0    EQU            0000_0000_0000_0000b

; A helper macro, making it easy to set all the fields of a Selector.
; The parameters are: Table Index, GDT or LDT, and RPL0-3
%define         Selector(Index, TI, RPL) ( Index + (x86.Seg.%+ TI | x86.Seg.%+ RPL) )