Working GDT?

git-svn-id: https://spexeah.com:8443/svn/Asuro@29 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c
This commit is contained in:
kieron 2017-05-16 18:09:59 +00:00
parent cc99ec9af5
commit 372c5ef010
23 changed files with 58 additions and 88 deletions

BIN
Asuro.iso

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,101 +1,71 @@
unit gdt; unit gdt;
interface interface
uses uses
util, types;
types,
console;
procedure create(base : dword; limit : dword; flags : byte);
procedure init();
type type
TSegmentDescriptor = bitpacked record TGDT_Entry = bitpacked record
limit_low : WORD; limit_low : int16;
base_low : WORD; base_low : int16;
access : Byte; base_middle : int8;
limit_n_flags : Byte; access : int8;
base_high : Byte; granularity : int8;
base_high : int8;
end; end;
PSegmentDescriptor = ^TSegmentDescriptor; PGDT_Entry = ^TGDT_Entry;
TGDT_Pointer = bitpacked record
limit : int16;
base : int32;
end;
var
gdt_entries : array[0..4] of TGDT_Entry;
gdt_pointer : TGDT_Pointer;
procedure init();
implementation implementation
var procedure flush_gdt(gdt_pointer : int32); assembler; nostackframe;
GDTarr : array [0..2] of TSegmentDescriptor; asm
GDTptr : PSegmentDescriptor; MOV EAX, gdt_pointer
GDT_length : integer = 0; LGDT [EAX]
MOV AX, $10
procedure create(base : dword; limit : dword; flags : byte); MOV DS, AX
var MOV ES, AX
descriptor : array[0..8] of Byte; MOV FS, AX
descriptor_ptr : PByte; MOV GS, AX
MOV SS, AX
s_descriptor : TSegmentDescriptor; db $EA // Bypass stupid inline ASM restrictions-
s_descriptor_ptr : PSegmentDescriptor; dw @@flush, $08 // by assembling the farjump instruction ourselves.
// It's just data, honest gov'.
@@flush:
RET
end;
procedure set_gate(Gate_Number : int32; Base : int32; Limit : int32; Access : int8; Granularity : int8);
begin begin
descriptor_ptr:= @descriptor[0]; gdt_entries[Gate_Number].base_low := (Base AND $FFFF);
s_descriptor_ptr:= @s_descriptor; gdt_entries[Gate_Number].base_middle := (Base SHR 16) AND $FF;
if limit <= 65536 then begin gdt_entries[Gate_Number].base_high := (Base SHR 24) AND $FF;
descriptor[6] := $40; gdt_entries[Gate_Number].limit_low := (Limit AND $FFFF);
end else begin gdt_entries[Gate_Number].granularity := ((Limit SHR 16) AND $0F) OR (Granularity AND $F0);
if (limit and $FFF) <> $FFF then begin gdt_entries[Gate_Number].access := Access;
limit := (limit SHR 12) - 1;
end else begin
limit := limit SHR 12;
end;
descriptor[6] := $C0;
end;
descriptor[0] := limit and $FF;
descriptor[1] := (limit shr 8) and $FF;
descriptor[6] := descriptor[6] or ((limit shr 16) and $F);
descriptor[2] := base and $FF;
descriptor[3] := (base shr 8) and $FF;
descriptor[4] := (base shr 16) and $FF;
descriptor[7] := (base shr 24) and $FF;
descriptor[5] := flags;
asm
MOV EAX, descriptor_ptr
MOV s_descriptor_ptr, EAX
end;
descriptor_ptr:= @descriptor[4];
asm
MOV EAX, descriptor_ptr
MOV EBX, s_descriptor_ptr
ADD EBX, 1
MOV [EBX], EAX
end;
GDTarr[GDT_length] := s_descriptor;
GDT_length := GDT_length + 1;
end; end;
procedure init(); procedure init();
var
data_addr : dword;
begin begin
GDTptr := @GDTarr[0]; gdt_pointer.limit := (sizeof(TGDT_Entry) * 5) - 1;
data_addr := (@GDTarr[2] - @GDTarr[0]) shr 16; gdt_pointer.base := int32(@gdt_entries);
create($0000, $0000, $00); //null segment / GDT pointer set_gate($00, $00, $00, $00, $00);
create($0000, $FFFF, $9A); //code descriptor set_gate($01, $00, $FFFFFFFF, $9A, $CF);
create($0000, $FFFF, $92); //data descriptor set_gate($02, $00, $FFFFFFFF, $92, $CF);
asm set_gate($03, $00, $FFFFFFFF, $FA, $CF);
LGDT GDTptr set_gate($04, $00, $FFFFFFFF, $F2, $CF);
MOV EAX, data_addr flush_gdt(int32(@gdt_pointer))
MOV DS, AX
MOV SS, AX
MOV ES, AX
MOV FS, AX
MOV GS, AX
end;
end; end;
end. end.

View File

@ -3,12 +3,12 @@ unit system;
interface interface
type type
cardinal = 0..$FFFFFFFF; cardinal = 0..$FFFFFFFF;
hresult = cardinal; hresult = cardinal;
dword = cardinal; dword = cardinal;
integer = longint; integer = longint;
pchar = ^char; pchar = ^char;
implementation implementation