home *** CD-ROM | disk | FTP | other *** search
- unit GetGDTW;
- {
- Refer Undocumented Windows #356 (sgdt) and
- Windows Tech Journal #28 1/93 and
- The Processor and Coprocessor #96/99 and refer text
- }
-
- interface
-
- uses
- Winprocs, SysUtils, StdCtrls;
-
-
- procedure FillList1(LBox: TListBox);
-
- implementation
-
- type
- AccessedStr = array[0..3] of char; {bit 0 $01 - AccessRights}
- DataWriteStr = array[0..4] of char; {bit 1 $02 - AccessRights}
- CodeReadStr = array[0..4] of char; {bit 1 $02 - AccessRights}
- ConformingStr = array[0..3] of char; {bit 2 $04 - AccessRights}
- UpDownStr = array[0..3] of char; {bit 2 $04 - AccessRights}
- CodeDataStr = array[0..3] of char; {bit 3 $08 - AccessRights}
- DescTypeStr = array[0..3] of char; {bit 4 $10 - AccessRights}
- PresentStr = array[0..3] of char; {bit 7 $80 - AccessRights}
- Bit5Str = array[0..3] of char; {bit 5 $20 - LimitH_Flags}
- DefSizeStr = array[0..4] of char; {bit 6 $40 - LimitH_Flags}
- GranStr = array[0..3] of char; {bit 7 $80 - LimitH_Flags}
- TSystemDesc = array[0..25] of Char;
-
- PShowDesc = ^TShowDesc; {---------SYSTEM DESCRIPTORS----------}
- TShowDesc = record {INT/TRAP CALL TASK gate }
- LimitL, {<---- offset low ----> reserved }
- BaseL: word; {<-- code segm. sel --> TSS segm. sel.}
- BaseM, {<--- bits 0/2=0 ---->, bits 0/7=res.
- 3/7=count 3/7=reserved }
- AccessRights: byte;
- {data/stack segm. code segment
- bit 0 0=not accessed/1=accessed bits 0/3: 0=reserved, 1=available 16b TSS
- bit 1 writable readable 2=ldt, 3=busy 16b TSS, 4=16b call gate
- bit 2 expand down conforming 5=16b task gate, 6=16b int. gate,
- bit 3 always 0 always 1 7=16b trap gate, 8=reserved, 9=avail.32b TSS
- 10=reserved, 11=busy 32b TSS, 12=32b call gate
- 13=reserved, 14=32b int.gate, 15=32b task gate
- bit 4 0=system desc./1=segm.descriptor <------------ 0 ---------------->
- bit 5+6 descriptor privilege level <------------- dpl --------------->
- bit 7 present <----------- present ------------->}
- LimitH_Flags, {LimitH_Flags+BaseH = }
- {80386/486 only, must be 0 on 80286} {offset (High) for Trap/Call ... }
- {bit 0/4 LimitH} {... gate, reserved for Task gate}
- {bit 5 available
- bit 6 default size 16/32 bits
- bit 7 granularity}
- BaseH: byte;
- {80386/486 only, must be 0 on 80286}
- end;
-
- PGDTMap =^TGDTMap;
- TGDTMap = array[0..65521 div Sizeof(TShowDesc)] of TShowDesc;
-
- LongRemap = record
- case Word of
- 0:(Long: LongInt);
- 1:(LoWord, HiWord: Word);
- end;
-
- const
- Accessed: array[boolean] of AccessedStr = (' Na',' Ac');
- DataWrite: array[boolean] of DataWriteStr = (' R ',' R/W');
- CodeRead: array[boolean] of CodeReadStr = (' NR',' R');
- Conforming: array[boolean] of ConformingStr = (' NC',' C');
- UpDown: array[boolean] of UpDownStr = (' Up',' Do');
- CodeData: array[boolean] of CodeDataStr = (' Da',' Co');
- DescType: array[boolean] of DescTypeStr =(' Sy',' Se');
- Present: array[boolean] of PresentStr = (' Nl',' Lo');
- Bit5: array[boolean] of Bit5Str = (' 5f',' 5t');
- DefSize: array[boolean] of DefSizeStr = (' 16b',' 32b');
- {default operands/adress size}
- Gran: array[boolean] of Granstr = (' 1b',' 4k'); {granularity}
- SystemDescType: array[0..15] of TSystemDesc =
- ('Reserved - type field: ', 'Available 16b TSS', 'LDT', 'Busy 16b TSS', '16b Call Gate',
- '16b Task Gate', '16b Int. Gate', '16b Trap gate', 'Reserved - type field: ',
- 'Available 32b TSS', 'Reserved - type field: ', 'Busy 32b TSS', '32b Call Gate',
- 'Reserved - type field: ', '32b Int. Gate', '32b Task Gate');
-
-
- var SGDTable, DummyStr:Array[0..79] of Char;
- PGDTable: PShowDesc;
- GDTable: TShowDesc;
- GDTMap: PGDTMap;
- Selector, _ds: Word;
- Typ, DPL: byte;
- DummyBase, DummyLimit,
- LDTBase, LDTLimit: Longint;
- Loop, LoopLimit: Integer;
- OutFile: Text;
-
-
- function HexB(Dest: PChar; B: Byte): PChar;
- const
- h: array[0..15] of Char = '0123456789ABCDEF';
- begin
- Dest[0] := h[b shr 4];
- Dest[1] := h[b and $0F];
- Dest[2] := #0;
- HexB := Dest;
- end;
-
- function HexW(Dest: PChar; I: Word): PChar;
- begin
- HexB(Dest, Hi(I));
- HexB(@Dest[2], Lo(I));
- HexW := Dest;
- end;
-
- function HexL(Dest: PChar; l:LongInt): PChar;
- var
- lr: LongRemap absolute l;
- begin
- HexW(Dest, lr.HiWord);
- HexW(@Dest[4], lr.LoWord);
- HexL := Dest;
- end;
-
- function ShowGDT(var GlDescTable: TShowDesc): String;
- var
- DummyAccess: Word;
- MyString: String;
- begin
- MyString := '';
- with GlDescTable do
- case (AccessRights and $10) of
- 0: begin
- StrCopy(SGDTable, SystemDescType[AccessRights and $F]);
- case (AccessRights and $F) of
- 0, 8, 10, 13: {Reserved}
- begin
- HexB(DummyStr, AccessRights and $F);
- StrCat(SGDTable, DummyStr);
- if (LimitL=0) and (BaseL=0) and (BaseM=0) and (AccessRights=0)
- and (LimitH_Flags=0) and (BaseH=0)
- then StrCat(SGDTable, ' - All zero''s ')
- else StrCat(SGDTable, ' - Not all zero''s ');
- end;
- 1, 3: {16b Task State Segment}
- begin
- {no further details displayed yet - refer #319}
- end;
- 9, 11: {32b Task State Segment}
- begin
- {no further details displayed yet - refer #320}
- end;
- 5, 15: {Task Gate}
- begin
- StrCat(SGDTable, ' TSS Segment Selector: ');
- HexW(DummyStr, BaseL);
- StrCat(SGDTable, DummyStr);
- end;
- 4, 6, 7, 12, 14: {Int/Trap/Call Gate}
- begin
- StrCat(SGDTable, '/Segment Sel. ');
- HexW(DummyStr, BaseL);
- StrCat(SGDTable, DummyStr);
- StrCat(SGDTable, '/Limit ');
- HexL(DummyStr, Longint(LimitL) + LongInt(LimitH_Flags) shl 16
- + LongInt(BaseH) shl 24);
- StrCat(SGDTable, DummyStr); {32 bits significant #312}
- StrCat(SGDTable, '/Dpl ');
- DPL:=(AccessRights shr 5) and $03;
- HexB(DummyStr, DPL);
- StrCat(SGDTable, @DummyStr[1]);
- StrCat(SGDTable, '/'); {is TARGET code segment present?}
- StrCat(SGDTable, Present[AccessRights and $80 > 0]);
- StrCat(SGDTable, '/Count ');
- HexB(DummyStr, BaseM and $F8); {5 bits significant #315}
- StrCat(SGDTable, DummyStr);
- end;
- 2: {LDT}
- begin
- LDTBase:=longint(BaseL) + longint(BaseM) shl 16 + longint(BaseH) shl 24;
- LDTLimit:=longint(LimitL) + longint(LimitH_Flags and $1F) shl 16;
- StrCat(SGDTable, '/Segment Sel. ');
- HexW(DummyStr, BaseL);
- StrCat(SGDTable, DummyStr);
- StrCat(SGDTable, '/Base ');
- HexL(DummyStr, LDTBase);
- StrCat(SGDTable, DummyStr);
- StrCat(SGDTable, '/Limit ');
- HexL(DummyStr, Longint(LimitL) + LongInt(LimitH_Flags) shl 16
- + LongInt(BaseH) shl 24);
- StrCat(SGDTable, @DummyStr[3]); {only 20 bits can be significant}
- StrCat(SGDTable, '/Dpl ');
- DPL:=(AccessRights shr 5) and $03;
- HexB(DummyStr, DPL);
- StrCat(SGDTable, @DummyStr[1]);
- end;
- end;
- end;
- else
- begin
- DummyBase:=longint(BaseL) + longint(BaseM) shl 16 + longint(BaseH) shl 24;
- DummyLimit:=longint(LimitL) + longint(LimitH_Flags and $1F) shl 16;
- HexL(SGDTable, DummyBase);
- MyString := 'Base ' + StrPas(SGDTable);
- { Write(OutFile, 'Base ', SGDTable); }
- HexL(DummyStr, DummyLimit);
- StrCopy(SGDTable, @DummyStr[3]); {only 20 bits can be significant}
- MyString := MyString + ' Limit ';
- { Write(OutFile, ' Limit '); }
- StrCat(SGDTable, Accessed[AccessRights and $01 > 0]);
- if AccessRights and $08 > 0 then {code segment}
- begin
- StrCat(SGDTable, CodeRead[AccessRights and $02 > 0]);
- StrCat(SGDTable, Conforming[AccessRights and $04 > 0]);
- StrCat(SGDTable, CodeData[True]);
- end
- else begin {data/stack segment}
- StrCat(SGDTable, DataWrite[AccessRights and $02 > 0]);
- StrCat(SGDTable, UpDown[Typ and $04 > 0]);
- StrCat(SGDTable, CodeData[False]);
- end;
- StrCat(SGDTable, DescType[AccessRights and $10 > 0]);
- StrCat(SGDTable, ' ');
- DPL:=(AccessRights shr 5) and $03;
- HexB(DummyStr, DPL);
- StrCat(SGDTable, @DummyStr[1]);
- StrCat(SGDTable, Present[AccessRights and $80 > 0]);
- StrCat(SGDTable, Bit5[LimitH_Flags and $20 > 0]);
- StrCat(SGDTable, DefSize[LimitH_Flags and $40 > 0]);
- StrCat(SGDTable, Gran[LimitH_Flags and $80 > 0]);
- end;
- end;
- MyString := MyString + SGDTAble;
- ShowGDT := MYString;
- {Writeln(OutFile, SGDTable); }
- end;
-
- function GetGDTMap(var Size: integer; Sel: Word): PGDTMap;
- var
- AResult: PGDTMap;
- {Thanks to Dj Murdoch for the idea of the 'sizing' method}
- begin
- AResult := GlobalLock(Sel);
- Size:=(GetSelectorLimit(Sel)+1) div SizeOf(TShowDesc);
- GetGDTMap := AResult;
- end;
-
- procedure StartList(LBox: TListBox);
- var
- MyString: String;
- begin
- Assign(OutFile, 'dtable.txt');
- Rewrite(OutFile);
- PGDTable:=@GDTable;
- asm
- les bx, PGDTable
- sgdt es:[bx]
- mov _ds,ds
- end;
- Selector:=AllocSelector(_ds);
- with GDTable do begin
- DummyBase:=longint(BaseL) + longint(BaseM) shl 16 + longint(AccessRights) shl 24;
- { Not regular descriptor, but just limit + base !!!!}
- DummyLimit:=longint(LimitL) + longint(LimitH_Flags and $1F) shl 16;
- SetSelectorBase(Selector, DummyBase);
- SetSelectorLimit(Selector, DummyLimit);
- GDTMap:=GetGDTMap(LoopLimit, Selector);
- { Writeln(OutFile, ' GDT Descriptor table at: '); }
- HexL(SGDTable, DummyBase);
- { Write(OutFile, 'Base ', SGDTable); }
- HexL(DummyStr, DummyLimit);
- StrCopy(SGDTable, @DummyStr[3]);
- { Writeln(OutFile,' Limit ', SGDTable);
- Writeln(OutFile,'Number of entries: '); }
- end;
- for Loop:=0 to LoopLimit-1 do
- LBox.Items.Add(ShowGDT(GDTMap^[loop])); {walk the GDT array}
-
- with GDTable do
- begin
- SetSelectorBase(Selector, LDTBase);
- SetSelectorLimit(Selector, LDTLimit);
- GDTMap:=GetGDTMap(LoopLimit, Selector);
-
-
- { Writeln(OutFile, ' LDT Descriptor table at: '); }
- HexL(SGDTable, LDTBase);
- { Write(OutFile, 'Base ', SGDTable); }
- LBox.Items.Add('==========================================');
- LBox.Items.Add('==========================================');
- MyString := ' LDT Descriptor table at: ' + StrPas(SGDTable);
- LBox.Items.Add(MyString);
-
- HexL(DummyStr, LDTLimit);
- StrCopy(SGDTable, @DummyStr[3]);
- Writeln(OutFile,' Limit ', SGDTable);
- Writeln(OutFile,'Number of entries: ');
- end;
- for Loop:=0 to LoopLimit-1 do
- LBox.Items.Add(ShowGDT(GDTMap^[loop])); {walk the GDT array}
-
- GlobalUnlock(Selector);
- FreeSelector(Selector);
- Close(OutFile);
- end;
-
- procedure FillList1(LBox: TListBox);
- begin
- StartList(LBox);
- end;
-
- end.
-
-