LogicMemoryManager working.

git-svn-id: https://spexeah.com:8443/svn/Asuro@159 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c
This commit is contained in:
kieron 2017-05-20 22:53:27 +00:00
parent 0a26e076b9
commit 87094a7afd
10 changed files with 118 additions and 74 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.

View File

@ -14,8 +14,9 @@ uses
bios_data_area, bios_data_area,
keyboard, keyboard,
vmemorymanager, vmemorymanager,
pmemorymanager; pmemorymanager,
//scheduler; lmemorymanager,
scheduler;
procedure kmain(mbinfo: Pmultiboot_info_t; mbmagic: uint32); stdcall; procedure kmain(mbinfo: Pmultiboot_info_t; mbmagic: uint32); stdcall;
@ -50,32 +51,22 @@ begin
isr.init(); isr.init();
irq.init(); irq.init();
pmemorymanager.init(); pmemorymanager.init();
//while true do begin end;
vmemorymanager.init(); vmemorymanager.init();
lmemorymanager.init();
vmemorymanager.new_page(0); scheduler.init();
pint:= puint32(0);
console.writestringln('Writing 1234 to Logical Address $00000000');
pint^:= 1234;
if pint^ = 1234 then begin
console.writestringln('Read 1234 back from Logical Address $00000000!!!');
end;
//scheduler.init();
STI; STI;
isr32.hook(uint32(@bios_data_area.tick_update)); isr32.hook(uint32(@bios_data_area.tick_update));
{z:= 1; {z:= 1;
while true do begin while true do begin
console.writeword(z); console.writehex(z);
console.writestring(': '); console.writestring(': ');
pint:= kalloc(1024*4); pint:= kalloc(1024*4);
console.writewordln(uint32(pint)); console.writehexln(uint32(pint));
if pint = nil then while true do begin end else pint^:= 1234; if pint = nil then while true do begin end else pint^:= 1234;
//kfree(pint);
z:=z+1; z:=z+1;
end;} end;}

View File

@ -7,98 +7,150 @@ uses
vmemorymanager, vmemorymanager,
console; console;
type
TBlock_Entry = packed record
Present : Boolean;
Length : uint8;
end;
const const
ALLOC_SPACE = 8; //64-Bit Allocations ALLOC_SPACE = 8; //64-Bit Allocations
MAX_ENTRIES = 0x60000; MAX_ENTRIES = $60000;
DATA_OFFSET = $100000;
type
THeapEntry = bitpacked record
Present : Boolean;
Root : Boolean;
Last : Boolean;
Resv1 : Boolean;
Resv2 : Boolean;
Resv3 : Boolean;
Resv4 : Boolean;
Resv5 : Boolean;
end;
THeapPage = packed record
Next_Page : uint32;
Prev_Page : uint32;
Entries : Array[0..MAX_ENTRIES-1] of THeapEntry;
end;
PHeapPage = ^THeapPage;
var var
Memory_Start : uint32; Root_Page : PHeapPage;
Memory_Manager : packed array[1..MAX_ENTRIES] of TBlock_Entry;
procedure init; procedure init;
function kalloc(size : uint32) : void;
procedure kfree(area : void);
implementation implementation
function new_lmm_page() : uint32;
var
i : integer;
begin
i:= KERNEL_PAGE_NUMBER + 4;
while not vmemorymanager.new_page(i) do begin
i:= i + 1;
end;
new_lmm_page:= i SHL 22;
end;
function new_heap_page(CurrentPage : PHeapPage) : PHeapPage;
var
i : integer;
begin
new_heap_page:= PHeapPage(new_lmm_page);
if CurrentPage <> nil then CurrentPage^.Next_Page:= uint32(new_heap_page);
new_heap_page^.Next_Page:= 0;
new_heap_page^.Prev_Page:= uint32(CurrentPage);
For i:=0 to MAX_ENTRIES-1 do begin
with new_heap_page^.Entries[i] do begin
Present:= False;
Root:= False;
Last:= False;
end;
end;
end;
procedure init; procedure init;
var var
i : uint32; i : uint32;
begin begin
console.writestringln('LMM: INIT BEGIN.'); Root_Page:= PHeapPage(new_lmm_page);
Root_Page^.Next_Page:= 0;
Root_Page^.Prev_Page:= 0;
For i:=0 to MAX_ENTRIES-1 do begin For i:=0 to MAX_ENTRIES-1 do begin
Memory_Manager[i].Present:= False; Root_Page^.Entries[i].Present:= False;
Root_Page^.Entries[i].Root:= False;
Root_Page^.Entries[i].Last:= False;
end; end;
Memory_Start:= uint32(@util.endptr);
console.writestringln('LMM: INIT END.');
end; end;
function kalloc(size : uint32) : void; function kalloc(size : uint32) : void;
var var
blocks : uint32; Heap_Entries : uint32;
rem : uint32; i, j : uint32;
i,j : uint32; hp : PHeapPage;
miss : boolean; miss : boolean;
begin begin
blocks:= size div ALLOC_SPACE; Heap_Entries:= size div 8;
rem:= size - (blocks * ALLOC_SPACE); If sint32(size-(Heap_Entries*8)) > 0 then Heap_Entries:= Heap_Entries + 1;
if rem > 0 then blocks:= blocks + 1; hp:= Root_Page;
kalloc:= nil; kalloc:= nil;
while kalloc = nil do begin
for i:=0 to MAX_ENTRIES-1 do begin for i:=0 to MAX_ENTRIES-1 do begin
miss:= false; miss:= false;
for j:=0 to blocks-1 do begin for j:=0 to Heap_Entries-1 do begin
if Memory_Manager[i+j].Present then begin if hp^.Entries[i+j].Present then begin
miss:= true; miss:= true;
break; break;
end; end;
end; end;
if not miss then begin if not miss then begin
kalloc:= void(Memory_Start+(i * ALLOC_SPACE)); kalloc:= void( uint32( hp ) + DATA_OFFSET + (i * 8) );
for j:=0 to blocks-1 do begin for j:=0 to Heap_Entries-1 do begin
Memory_Manager[i+j].Present:= true; hp^.Entries[i+j].Present:= True;
Memory_Manager[i+j].Length:= 0; if j = (Heap_Entries-1) then begin
if j = 0 then Memory_Manager[i+j].Length:= blocks; hp^.Entries[i+j].Last:= True;
end;
if j = 0 then begin
hp^.Entries[i+j].Root:= True;
end;
end; end;
console.writestring('Allocated ');
console.writeint(blocks);
console.writestring(' Block(s). [Block: ');
console.writeint(i);
console.writestringln(']');
break; break;
end; end;
end; end;
if kalloc = nil then begin
if PHeapPage(hp^.Next_Page) = nil then begin
new_heap_page(hp);
end;
hp:= PHeapPage(hp^.Next_Page);
end;
end;
end; end;
procedure kfree(area : void); procedure kfree(area : void);
var var
Block : uint32; hp : PHeapPage;
bLength : uint8; entry : uint32;
i : uint32;
begin begin
if uint32(area) < Memory_Start then begin hp:= PHeapPage((uint32(area) SHR 22) SHL 22);
asm entry:= (uint32(area) - DATA_OFFSET - uint32(hp)) div 8;
INT 13 if hp^.Entries[entry].Present then begin
while not hp^.Entries[entry].Root do begin
entry:= entry - 1;
end; end;
While not hp^.Entries[entry].Last do begin
hp^.Entries[entry].Present:= False;
hp^.Entries[entry].Root:= False;
hp^.Entries[entry].Last:= False;
entry:= entry + 1;
end; end;
Block:= (uint32(Area) - Memory_Start) div ALLOC_SPACE; hp^.Entries[entry].Present:= False;
if Memory_Manager[Block].Present then begin hp^.Entries[entry].Root:= False;
If Memory_Manager[Block].Length > 0 then begin hp^.Entries[entry].Last:= False;
bLength:= Memory_Manager[Block].Length;
for i:=0 to bLength-1 do begin
Memory_Manager[Block+i].Present:= False;
end;
end else begin end else begin
asm GPF;
INT 13
end;
end;
end; end;
end; end;

View File

@ -60,6 +60,7 @@ begin
PageDirectory:= load_current_page_directory; PageDirectory:= load_current_page_directory;
map_page(KERNEL_PAGE_NUMBER + 1, 1); map_page(KERNEL_PAGE_NUMBER + 1, 1);
map_page(KERNEL_PAGE_NUMBER + 2, 2); map_page(KERNEL_PAGE_NUMBER + 2, 2);
map_page(KERNEL_PAGE_NUMBER + 3, 3);
console.writestringln('VMM: INIT END.'); console.writestringln('VMM: INIT END.');
end; end;