TempleOSCDV5.00.ISO
This commit is contained in:
1 parent
122df6338e
commit
f6eacf7744
374 files changed
+11078
-11204
No files matched your search
+15
-14
@@ -1,35 +1,35 @@
|
||||
U0 BlkPoolAdd(CBlkPool *bp,CMemBlk *m,I64 pages512)
|
||||
U0 BlkPoolAdd(CBlkPool *bp,CMemBlk *m,I64 pags)
|
||||
{//Add mem to BlkPool.
|
||||
if (sys_mem_init_flag)
|
||||
MemSet(m,sys_mem_init_val,pages512*512);
|
||||
MemSet(m,sys_mem_init_val,pags*MEM_PAG_SIZE);
|
||||
PUSHFD
|
||||
CLI
|
||||
while (LBts(&bp->locked_flags,BPlf_LOCKED))
|
||||
PAUSE
|
||||
m->next=bp->mem_free_lst;
|
||||
m->pages=pages512;
|
||||
m->pags=pags;
|
||||
m->mb_signature=MBS_UNUSED_SIGNATURE_VAL;
|
||||
bp->alloced_u8s+=pages512<<MEM_PAGE_BITS;
|
||||
bp->alloced_u8s+=pags<<MEM_PAG_BITS;
|
||||
bp->mem_free_lst=m;
|
||||
LBtr(&bp->locked_flags,BPlf_LOCKED);
|
||||
POPFD
|
||||
}
|
||||
|
||||
U0 BlkPoolInit(CBlkPool *bp,I64 pages512)
|
||||
U0 BlkPoolInit(CBlkPool *bp,I64 pags)
|
||||
{//Make mem chunk into a BlkPool.
|
||||
I64 num;
|
||||
CMemBlk *m;
|
||||
MemSet(bp,0,sizeof(CBlkPool));
|
||||
m=(bp(U8 *)+sizeof(CBlkPool)+MEM_PAGE_SIZE-1)&~(MEM_PAGE_SIZE-1);
|
||||
num=(bp(U8 *)+pages512<<MEM_PAGE_BITS-m(U8 *))>>MEM_PAGE_BITS;
|
||||
bp->alloced_u8s=(pages512-num)<<MEM_PAGE_BITS; //Compensate before num added.
|
||||
m=(bp(U8 *)+sizeof(CBlkPool)+MEM_PAG_SIZE-1)&~(MEM_PAG_SIZE-1);
|
||||
num=(bp(U8 *)+pags<<MEM_PAG_BITS-m(U8 *))>>MEM_PAG_BITS;
|
||||
bp->alloced_u8s=(pags-num)<<MEM_PAG_BITS; //Compensate before num added.
|
||||
BlkPoolAdd(bp,m,num);
|
||||
}
|
||||
|
||||
U0 BlkPoolsInit()
|
||||
{
|
||||
I64 i,total,lo,hi,code_heap_limit;
|
||||
CMemE820 *m20=SYS_MEM_E820;
|
||||
CMemE820 *m20=MEM_E820;
|
||||
Bool first=TRUE;
|
||||
|
||||
total=BIOSTotalMem;
|
||||
@@ -41,9 +41,9 @@ U0 BlkPoolsInit()
|
||||
else
|
||||
code_heap_limit=0x80000000;
|
||||
|
||||
i=code_heap_limit-0x1000000;
|
||||
BlkPoolAdd(sys_code_bp,0x1000000,i>>MEM_PAGE_BITS);
|
||||
sys_heap_limit=i+0x1000000-1;
|
||||
i=code_heap_limit-SYS_16MEG_AREA_LIMIT; //See $LK,"RLf_16MEG_SYS_CODE_BP",A="FF:::/Kernel/Mem/PageTables.HC,RLf_16MEG_SYS_CODE_BP"$
|
||||
BlkPoolAdd(sys_code_bp,SYS_16MEG_AREA_LIMIT,i>>MEM_PAG_BITS);
|
||||
mem_heap_limit=i+SYS_16MEG_AREA_LIMIT-1;
|
||||
|
||||
if (code_heap_limit<total) {
|
||||
while (m20->type) {
|
||||
@@ -58,15 +58,16 @@ U0 BlkPoolsInit()
|
||||
}
|
||||
if (code_heap_limit<=lo<hi) {
|
||||
if (first) {
|
||||
BlkPoolInit(lo,(hi-lo)>>MEM_PAGE_BITS);
|
||||
BlkPoolInit(lo,(hi-lo)>>MEM_PAG_BITS);
|
||||
sys_data_bp=lo;
|
||||
Fs->data_heap=HeapCtrlInit(,Fs,sys_data_bp);
|
||||
first=FALSE;
|
||||
} else
|
||||
BlkPoolAdd(sys_data_bp,lo,(hi-lo)>>MEM_PAGE_BITS);
|
||||
BlkPoolAdd(sys_data_bp,lo,(hi-lo)>>MEM_PAG_BITS);
|
||||
}
|
||||
}
|
||||
m20++;
|
||||
}
|
||||
}
|
||||
LBts(&sys_run_level,RLf_FULL_HEAPS);
|
||||
}
|
||||
@@ -23,7 +23,7 @@ U0 HeapCtrlDel(CHeapCtrl *hc)
|
||||
m=hc->next_mem_blk;
|
||||
while (m!=&hc->next_mem_blk) {
|
||||
m1=m->next;
|
||||
Mem512TaskFree(m,hc);
|
||||
MemPagTaskFree(m,hc);
|
||||
m=m1;
|
||||
}
|
||||
LBtr(&hc->locked_flags,HClf_LOCKED);
|
||||
|
||||
+18
-18
@@ -51,20 +51,20 @@ _MALLOC::
|
||||
JMP I32 MALLOC_ALMOST_DONE
|
||||
|
||||
//Big allocation
|
||||
@@30: ADD RAX,sizeof(CMemBlk)+MEM_PAGE_SIZE-1
|
||||
SHR RAX,MEM_PAGE_BITS
|
||||
@@30: ADD RAX,sizeof(CMemBlk)+MEM_PAG_SIZE-1
|
||||
SHR RAX,MEM_PAG_BITS
|
||||
|
||||
PUSH RDX //preserve HeapCtrl
|
||||
PUSH RDX
|
||||
PUSH RAX
|
||||
CALL &Mem512TaskAlloc
|
||||
CALL &MemPagTaskAlloc
|
||||
POP RDX
|
||||
TEST RAX,RAX
|
||||
JZ @@45 //Out of memory
|
||||
MOV RSI,RAX
|
||||
MOV EAX,U32 CMemBlk.pages[RSI]
|
||||
MOV EAX,U32 CMemBlk.pags[RSI]
|
||||
|
||||
SHL RAX,MEM_PAGE_BITS
|
||||
SHL RAX,MEM_PAG_BITS
|
||||
SUB RAX,sizeof(CMemBlk)
|
||||
ADD RSI,sizeof(CMemBlk)
|
||||
JMP I32 MALLOC_ALMOST_DONE
|
||||
@@ -77,13 +77,13 @@ _MALLOC::
|
||||
TEST RSI,RSI
|
||||
JNZ I32 @@60
|
||||
PUSH RAX //-**** save byte size
|
||||
ADD RAX,16*MEM_PAGE_SIZE-1
|
||||
SHR RAX,MEM_PAGE_BITS
|
||||
ADD RAX,16*MEM_PAG_SIZE-1
|
||||
SHR RAX,MEM_PAG_BITS
|
||||
|
||||
PUSH RDX //preserve HeapCtrl
|
||||
PUSH RDX
|
||||
PUSH RAX
|
||||
CALL &Mem512TaskAlloc
|
||||
CALL &MemPagTaskAlloc
|
||||
POP RDX
|
||||
TEST RAX,RAX
|
||||
JNZ @@50
|
||||
@@ -99,8 +99,8 @@ _MALLOC::
|
||||
JMP I32 MALLOC_FINAL_EXIT //Never gets here, hopefully.
|
||||
|
||||
@@50: MOV RSI,RAX
|
||||
MOV EAX,U32 CMemBlk.pages[RSI]
|
||||
SHL RAX,MEM_PAGE_BITS
|
||||
MOV EAX,U32 CMemBlk.pags[RSI]
|
||||
SHL RAX,MEM_PAG_BITS
|
||||
|
||||
//Can it be combined with last chunk? (Never Free these chunks.)
|
||||
MOV RDI,U64 CHeapCtrl.last_mergable[RDX]
|
||||
@@ -109,8 +109,8 @@ _MALLOC::
|
||||
JNE @@55
|
||||
|
||||
PUSH RAX
|
||||
MOV EAX,U32 CMemBlk.pages[RDI]
|
||||
ADD U32 CMemBlk.pages[RSI],EAX
|
||||
MOV EAX,U32 CMemBlk.pags[RDI]
|
||||
ADD U32 CMemBlk.pags[RSI],EAX
|
||||
//QueRem
|
||||
MOV RAX,U64 CMemBlk.next[RDI]
|
||||
MOV RBX,U64 CMemBlk.last[RDI]
|
||||
@@ -161,7 +161,7 @@ MALLOC_ALMOST_DONE:
|
||||
|
||||
//Caller1/Caller2
|
||||
PUSH RDX
|
||||
MOV RDX,U64 [SYS_HEAP_LIMIT]
|
||||
MOV RDX,U64 [MEM_HEAP_LIMIT]
|
||||
MOV RDI,U64 SF_RIP[RBP]
|
||||
CMP RDI,RDX
|
||||
JB @@80
|
||||
@@ -190,7 +190,7 @@ MALLOC_ALMOST_DONE:
|
||||
MOV U64 CMemUsed.hc[RSI],RDX
|
||||
LEA RAX,U64 CMemUsed.start[RSI]
|
||||
|
||||
TEST U8 [SYS_SEMAS+SYS_SEMA_HEAPLOG_ACTIVE*SEMA_STRUCT_SIZE],1
|
||||
TEST U8 [SYS_SEMAS+SEMA_HEAPLOG_ACTIVE*DFT_CACHE_LINE_WIDTH],1
|
||||
JZ @@105
|
||||
PUSH RAX
|
||||
PUSH RAX
|
||||
@@ -221,13 +221,13 @@ MALLOC_FINAL_EXIT:
|
||||
RET1 16
|
||||
//************************************
|
||||
_FREE::
|
||||
//Be aware of $LK,"heap_hash",A="FF:::/Kernel/Mem/MAllocFree.HC,heap_hash"$ in $LK,"Mem512TaskAlloc",A="MN:Mem512TaskAlloc"$().
|
||||
//Be aware of $LK,"heap_hash",A="FF:::/Kernel/Mem/MAllocFree.HC,heap_hash"$ in $LK,"MemPagTaskAlloc",A="MN:MemPagTaskAlloc"$().
|
||||
PUSH RBP
|
||||
MOV RBP,RSP
|
||||
PUSH RSI
|
||||
PUSH RDI
|
||||
|
||||
TEST U8 [SYS_SEMAS+SYS_SEMA_HEAPLOG_ACTIVE*SEMA_STRUCT_SIZE],1
|
||||
TEST U8 [SYS_SEMAS+SEMA_HEAPLOG_ACTIVE*DFT_CACHE_LINE_WIDTH],1
|
||||
JZ @@15
|
||||
MOV RBX,U64 SF_ARG1[RBP]
|
||||
TEST RBX,RBX
|
||||
@@ -285,7 +285,7 @@ _FREE::
|
||||
MOV U64 CMemUsed.next[RDI],RDX
|
||||
|
||||
//Caller1/Caller2
|
||||
MOV RDX,U64 [SYS_HEAP_LIMIT]
|
||||
MOV RDX,U64 [MEM_HEAP_LIMIT]
|
||||
MOV RDI,U64 SF_RIP[RBP]
|
||||
CMP RDI,RDX
|
||||
JB @@35
|
||||
@@ -321,7 +321,7 @@ _FREE::
|
||||
PUSH RDX
|
||||
PUSH RDX
|
||||
PUSH RSI
|
||||
CALL &Mem512TaskFree
|
||||
CALL &MemPagTaskFree
|
||||
POP RDX
|
||||
|
||||
@@55: LOCK
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#exe {Cd(__DIR__);};
|
||||
#include "PageTables"
|
||||
#include "Mem512"
|
||||
#include "MemPag"
|
||||
#include "BlkPool"
|
||||
#include "MAllocFree"
|
||||
#include "HeapCtrl"
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
U0 SysBadFree(I64 *ptr)
|
||||
{
|
||||
Dbg("Bad Free:",ptr);
|
||||
Panic("Bad Free:",ptr);
|
||||
}
|
||||
|
||||
U0 SysBadMAlloc(I64 *ptr)
|
||||
{
|
||||
Dbg("Bad MAlloc:",ptr);
|
||||
Panic("Bad MAlloc:",ptr);
|
||||
}
|
||||
|
||||
U8 *Mem512Alloc(I64 pages512,CBlkPool *bp=NULL)
|
||||
{/*Alloc 512Byte pages from BlkPool. Don't link to task.
|
||||
U8 *MemPagAlloc(I64 pags,CBlkPool *bp=NULL)
|
||||
{/*Alloc pags from BlkPool. Don't link to task.
|
||||
(Linking to a task means they will be freed when the task dies.)
|
||||
It might give you more than you asked for
|
||||
so a ptr to a page count is passed.
|
||||
It might give you more than you asked for.
|
||||
|
||||
Return: NULL if out of memory.
|
||||
*/
|
||||
@@ -23,23 +22,23 @@ Return: NULL if out of memory.
|
||||
CLI
|
||||
while (LBts(&bp->locked_flags,BPlf_LOCKED))
|
||||
PAUSE
|
||||
if (pages512<MEM_FREE_PAGE_HASH_SIZE) {
|
||||
if (res=bp->free_page_hash[pages512]) {
|
||||
bp->free_page_hash[pages512]=res->next;
|
||||
if (pags<MEM_FREE_PAG_HASH_SIZE) {
|
||||
if (res=bp->free_pag_hash[pags]) {
|
||||
bp->free_pag_hash[pags]=res->next;
|
||||
goto at_done;
|
||||
}
|
||||
i=Bsr(MEM_FREE_PAGE_HASH_SIZE)+1;
|
||||
i=Bsr(MEM_FREE_PAG_HASH_SIZE)+1;
|
||||
} else {
|
||||
//We'll now round-up to a power of two.
|
||||
//There is some overhead on allocations and
|
||||
//we wouldn't want to round to the next
|
||||
//power of two if a power of two was requested.
|
||||
//So we use a little more than a power of two.
|
||||
pages512-=MEM_EXTRA_HASH2_PAGES;
|
||||
i=Bsr(pages512)+1;
|
||||
pages512=1<<i+MEM_EXTRA_HASH2_PAGES;
|
||||
if (res=bp->free_page_hash2[i]) {
|
||||
bp->free_page_hash2[i]=res->next;
|
||||
pags-=MEM_EXTRA_HASH2_PAGS;
|
||||
i=Bsr(pags)+1;
|
||||
pags=1<<i+MEM_EXTRA_HASH2_PAGS;
|
||||
if (res=bp->free_pag_hash2[i]) {
|
||||
bp->free_pag_hash2[i]=res->next;
|
||||
goto at_done;
|
||||
}
|
||||
}
|
||||
@@ -49,84 +48,84 @@ Return: NULL if out of memory.
|
||||
//We're probably out of luck, but lets search for a
|
||||
//freed larger size block... and, screw-it, return the whole thing.
|
||||
do {
|
||||
if (res=bp->free_page_hash2[++i]) {
|
||||
pages512=1<<i+MEM_EXTRA_HASH2_PAGES;
|
||||
bp->free_page_hash2[i]=res->next;
|
||||
if (res=bp->free_pag_hash2[++i]) {
|
||||
pags=1<<i+MEM_EXTRA_HASH2_PAGS;
|
||||
bp->free_pag_hash2[i]=res->next;
|
||||
goto at_done;
|
||||
}
|
||||
} while (i<64-MEM_PAGE_BITS-1);
|
||||
pages512=0;
|
||||
} while (i<64-MEM_PAG_BITS-1);
|
||||
pags=0;
|
||||
res=NULL; //Out of memory
|
||||
goto at_done2;
|
||||
}
|
||||
if (res->pages<pages512)
|
||||
if (res->pags<pags)
|
||||
m=res;
|
||||
else {
|
||||
if (res->pages==pages512) {
|
||||
if (res->pags==pags) {
|
||||
m->next=res->next;
|
||||
goto at_done;
|
||||
} else {
|
||||
res->pages-=pages512;
|
||||
res(U8 *)+=res->pages<<MEM_PAGE_BITS;
|
||||
res->pages=pages512;
|
||||
res->pags-=pags;
|
||||
res(U8 *)+=res->pags<<MEM_PAG_BITS;
|
||||
res->pags=pags;
|
||||
goto at_done;
|
||||
}
|
||||
}
|
||||
}
|
||||
at_done:
|
||||
bp->used_u8s+=res->pages<<MEM_PAGE_BITS;
|
||||
bp->used_u8s+=res->pags<<MEM_PAG_BITS;
|
||||
at_done2:
|
||||
LBtr(&bp->locked_flags,BPlf_LOCKED);
|
||||
POPFD
|
||||
return res;
|
||||
}
|
||||
|
||||
U0 Mem512Free(CMemBlk *m,CBlkPool *bp=NULL)
|
||||
{//Return non-task 512Byte pages to BlkPool.
|
||||
I64 i,pages512;
|
||||
U0 MemPagFree(CMemBlk *m,CBlkPool *bp=NULL)
|
||||
{//Return non-task pags to BlkPool.
|
||||
I64 i,pags;
|
||||
if (m) {
|
||||
if (!bp) bp=sys_code_bp;
|
||||
PUSHFD
|
||||
CLI
|
||||
while (LBts(&bp->locked_flags,BPlf_LOCKED))
|
||||
PAUSE
|
||||
pages512=m->pages;
|
||||
pags=m->pags;
|
||||
m->mb_signature=MBS_UNUSED_SIGNATURE_VAL;
|
||||
bp->used_u8s-=pages512<<MEM_PAGE_BITS;
|
||||
if (pages512<MEM_FREE_PAGE_HASH_SIZE) {
|
||||
m->next=bp->free_page_hash[pages512];
|
||||
bp->free_page_hash[pages512]=m;
|
||||
bp->used_u8s-=pags<<MEM_PAG_BITS;
|
||||
if (pags<MEM_FREE_PAG_HASH_SIZE) {
|
||||
m->next=bp->free_pag_hash[pags];
|
||||
bp->free_pag_hash[pags]=m;
|
||||
} else {
|
||||
//We'll now round-up to a power of two.
|
||||
//There is some overhead on allocations and
|
||||
//we wouldn't want to round to the next
|
||||
//power of two if a power of two was requested.
|
||||
//So we use a little more than a power of two.
|
||||
pages512-=MEM_EXTRA_HASH2_PAGES;
|
||||
i=Bsr(pages512);
|
||||
m->next=bp->free_page_hash2[i];
|
||||
bp->free_page_hash2[i]=m;
|
||||
pags-=MEM_EXTRA_HASH2_PAGS;
|
||||
i=Bsr(pags);
|
||||
m->next=bp->free_pag_hash2[i];
|
||||
bp->free_pag_hash2[i]=m;
|
||||
}
|
||||
LBtr(&bp->locked_flags,BPlf_LOCKED);
|
||||
POPFD
|
||||
}
|
||||
}
|
||||
|
||||
CMemBlk *Mem512TaskAlloc(I64 pages512,CHeapCtrl *hc)
|
||||
{/*hc must be locked. Interrupts should probably be off
|
||||
CMemBlk *MemPagTaskAlloc(I64 pags,CHeapCtrl *hc)
|
||||
{/*hc must be locked. Don't preempt this routine.
|
||||
Currently, this is only called from $LK,"MAlloc",A="MN:MAlloc"$().
|
||||
Return: NULL if out of memory.
|
||||
*/
|
||||
CMemBlk *res;
|
||||
I64 threshold,cnt,size;
|
||||
CMemUnused *uum,**_uum,**_ptr;
|
||||
if (res=Mem512Alloc(pages512,hc->bp)) {
|
||||
if (res=MemPagAlloc(pags,hc->bp)) {
|
||||
QueIns(res,hc->last_mem_blk);
|
||||
res->mb_signature=MBS_USED_SIGNATURE_VAL;
|
||||
hc->alloced_u8s+=res->pages<<MEM_PAGE_BITS;
|
||||
hc->alloced_u8s+=res->pags<<MEM_PAG_BITS;
|
||||
|
||||
//Tidy-up free lst (Move into heap hash)
|
||||
//because if free lst gets long, delay causes crash
|
||||
//because if free lst gets long, delay causes crash.
|
||||
threshold=MEM_HEAP_HASH_SIZE>>4;
|
||||
#assert MEM_HEAP_HASH_SIZE>>4>=sizeof(U8 *)
|
||||
do {
|
||||
@@ -151,7 +150,7 @@ Return: NULL if out of memory.
|
||||
return res;
|
||||
}
|
||||
|
||||
U0 Mem512TaskFree(CMemBlk *m,CHeapCtrl *hc)
|
||||
U0 MemPagTaskFree(CMemBlk *m,CHeapCtrl *hc)
|
||||
{//hc must be locked
|
||||
if (m) {
|
||||
PUSHFD
|
||||
@@ -160,8 +159,8 @@ U0 Mem512TaskFree(CMemBlk *m,CHeapCtrl *hc)
|
||||
SysBadFree(m);
|
||||
else {
|
||||
QueRem(m);
|
||||
hc->alloced_u8s-=m->pages<<MEM_PAGE_BITS;
|
||||
Mem512Free(m,hc->bp);
|
||||
hc->alloced_u8s-=m->pags<<MEM_PAG_BITS;
|
||||
MemPagFree(m,hc->bp);
|
||||
}
|
||||
POPFD
|
||||
}
|
||||
+16
-39
@@ -30,7 +30,7 @@ Bool Mem32DevIns(CMemRange *tmpmr)
|
||||
U0 Mem32DevInit()
|
||||
{
|
||||
CMemRange *tmpmr;
|
||||
CMemE820 *m20=SYS_MEM_E820;
|
||||
CMemE820 *m20=MEM_E820;
|
||||
|
||||
QueInit(&dev.mem32_head);
|
||||
tmpmr=AMAlloc(sizeof(CMemRange));
|
||||
@@ -55,36 +55,13 @@ U0 Mem32DevInit()
|
||||
}
|
||||
}
|
||||
|
||||
U0 MemPagesNotPresentMark()
|
||||
{
|
||||
U8 *a,*max_physical=NULL;
|
||||
U16 *m01=SYS_MEM_E801;
|
||||
CMemE820 *m20=SYS_MEM_E820;
|
||||
while (m20->type) {
|
||||
a=m20->base+m20->len;
|
||||
if (a>max_physical)
|
||||
max_physical=a;
|
||||
m20++;
|
||||
}
|
||||
if (max_physical>=0x1000000+m01[1]<<16) {
|
||||
if (sys_gig_pages)
|
||||
a=(max_physical+0x3FFFFFFF)&~0x3FFFFFFF;
|
||||
else
|
||||
a=(max_physical+0x1FFFFF)&~0x1FFFFF;
|
||||
while (a<MEM_MAPPED_SPACE) {
|
||||
MemPagePresentMark(a,FALSE);
|
||||
a+=MemPageSize(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
U8 *Mem32DevAlloc(I64 size,I64 alignment)
|
||||
{//Alloc 32-bit addr space for device. (Doesn't work.) Not used.
|
||||
//For this to work the BIOS E820 map must be searched for gaps in
|
||||
//the 32-bit range and the pool initialized to the gaps.
|
||||
U8 *base,*limit;
|
||||
CMemRange *tmpmr,*tmpmr1;
|
||||
while (LBts(&sys_semas[SYS_SEMA_DEV_MEM],0))
|
||||
while (LBts(&sys_semas[SEMA_DEV_MEM],0))
|
||||
Yield;
|
||||
tmpmr1=dev.mem32_head.next;
|
||||
while (tmpmr1!=&dev.mem32_head) {
|
||||
@@ -99,15 +76,15 @@ U8 *Mem32DevAlloc(I64 size,I64 alignment)
|
||||
tmpmr->size=size;
|
||||
if (!Mem32DevIns(tmpmr)) {
|
||||
Free(tmpmr);
|
||||
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
|
||||
LBtr(&sys_semas[SEMA_DEV_MEM],0);
|
||||
return NULL;
|
||||
}
|
||||
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
|
||||
LBtr(&sys_semas[SEMA_DEV_MEM],0);
|
||||
return tmpmr->base;
|
||||
}
|
||||
tmpmr1=tmpmr1->next;
|
||||
}
|
||||
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
|
||||
LBtr(&sys_semas[SEMA_DEV_MEM],0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -115,7 +92,7 @@ U0 Mem32DevFree(U8 *base)
|
||||
{//Free 32-bit device address space.
|
||||
CMemRange *tmpmr;
|
||||
if (!base) return;
|
||||
while (LBts(&sys_semas[SYS_SEMA_DEV_MEM],0))
|
||||
while (LBts(&sys_semas[SEMA_DEV_MEM],0))
|
||||
Yield;
|
||||
tmpmr=dev.mem32_head.next;
|
||||
while (tmpmr!=&dev.mem32_head) {
|
||||
@@ -125,14 +102,14 @@ U0 Mem32DevFree(U8 *base)
|
||||
}
|
||||
tmpmr=tmpmr->next;
|
||||
}
|
||||
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
|
||||
LBtr(&sys_semas[SEMA_DEV_MEM],0);
|
||||
}
|
||||
|
||||
U8 *Mem64DevAlloc(I64 *_pages1Gig)
|
||||
{//Alloc 64-bit addr space for device.
|
||||
U8 *a;
|
||||
I64 i=*_pages1Gig,*pte;
|
||||
while (LBts(&sys_semas[SYS_SEMA_DEV_MEM],0))
|
||||
while (LBts(&sys_semas[SEMA_DEV_MEM],0))
|
||||
Yield;
|
||||
while (i--) {
|
||||
a=dev.mem64_ptr-=1<<30;
|
||||
@@ -140,22 +117,22 @@ U8 *Mem64DevAlloc(I64 *_pages1Gig)
|
||||
pte=MemPageTable(a);
|
||||
*pte=*pte&~0x18 |0x11; //Uncached and present
|
||||
InvlPg(dev.mem64_ptr);
|
||||
a+=MemPageSize(a);
|
||||
a+=mem_page_size;
|
||||
} while (a-dev.mem64_ptr<1<<30);
|
||||
}
|
||||
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
|
||||
LBtr(&sys_semas[SEMA_DEV_MEM],0);
|
||||
return dev.mem64_ptr;
|
||||
}
|
||||
|
||||
U0 Mem64DevFree(U8 *base,I64 pages1Gig)
|
||||
{//Free 64-bit device address space.
|
||||
if (!base) return;
|
||||
while (LBts(&sys_semas[SYS_SEMA_DEV_MEM],0))
|
||||
while (LBts(&sys_semas[SEMA_DEV_MEM],0))
|
||||
Yield;
|
||||
if (base==dev.mem64_ptr)
|
||||
dev.mem64_ptr+=pages1Gig*1<<30;
|
||||
//else not freed
|
||||
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
|
||||
LBtr(&sys_semas[SEMA_DEV_MEM],0);
|
||||
}
|
||||
|
||||
U0 UncachedAliasAlloc() //Make uncached alias for 4 lowest Gig.
|
||||
@@ -167,15 +144,15 @@ U0 UncachedAliasAlloc() //Make uncached alias for 4 lowest Gig.
|
||||
pte=MemPageTable(a);
|
||||
*pte=0x197+a-dev.uncached_alias;
|
||||
InvlPg(a);
|
||||
a+=MemPageSize(a);
|
||||
a+=mem_page_size;
|
||||
} while (a-dev.uncached_alias<1<<32);
|
||||
}
|
||||
|
||||
I64 BIOSTotalMem()
|
||||
{
|
||||
I64 r01,r20;
|
||||
U16 *m01=SYS_MEM_E801;
|
||||
CMemE820 *m20=SYS_MEM_E820;
|
||||
U16 *m01=MEM_E801;
|
||||
CMemE820 *m20=MEM_E820;
|
||||
|
||||
r01=0x100000+m01[0]<<10+m01[1]<<16;
|
||||
r20=0;
|
||||
@@ -201,4 +178,4 @@ I64 Scale2Mem(I64 min,I64 max,I64 limit=2*1024*1024*1024)
|
||||
return max;
|
||||
else
|
||||
return min+(max-min)*i/limit;
|
||||
}
|
||||
}
|
||||
+138
-87
@@ -3,152 +3,203 @@ asm {
|
||||
|
||||
USE32
|
||||
SYS_INIT_PAGE_TABLES::
|
||||
//Set page tables to identity map everything.
|
||||
//Check 1Gig page capability and set page size.
|
||||
MOV EAX,0x80000001
|
||||
CPUID
|
||||
MOV EAX,1<<21
|
||||
BT EDX,26
|
||||
JNC @@05
|
||||
MOV EAX,1<<30
|
||||
@@05: MOV U32 [MEM_PAGE_SIZE],EAX
|
||||
|
||||
//Set mapped space limit
|
||||
MOV EAX,[MEM_PHYSICAL_SPACE]
|
||||
MOV EDX,[MEM_PHYSICAL_SPACE+4]
|
||||
BT U32 [MEM_PAGE_SIZE],30 //Round-up to 1Gig boundary?
|
||||
JNC @@10
|
||||
ADD EAX,0x3FFFFFFF
|
||||
ADC EDX,0
|
||||
AND EAX,~0x3FFFFFFF
|
||||
@@10: INC EDX //Need 4Gig extra for uncached alias up at top of space.
|
||||
MOV [MEM_MAPPED_SPACE],EAX
|
||||
MOV [MEM_MAPPED_SPACE+4],EDX
|
||||
|
||||
//How many 2Meg pages?
|
||||
MOV CL,21
|
||||
ADD EAX,0x1FFFFF
|
||||
ADC EDX,0
|
||||
SHRD EAX,EDX
|
||||
SHR EDX,CL
|
||||
MOV [MEM_NUM_2MEG],EAX
|
||||
MOV [MEM_NUM_2MEG+4],EDX
|
||||
|
||||
//How many 1Gig pages?
|
||||
MOV CL,9
|
||||
ADD EAX,0x1FF
|
||||
ADC EDX,0
|
||||
SHRD EAX,EDX
|
||||
SHR EDX,CL
|
||||
MOV [MEM_NUM_1GIG],EAX
|
||||
MOV [MEM_NUM_1GIG+4],EDX
|
||||
|
||||
//How many 512Gig pages?
|
||||
MOV CL,9
|
||||
ADD EAX,0x1FF
|
||||
ADC EDX,0
|
||||
SHRD EAX,EDX
|
||||
SHR EDX,CL
|
||||
MOV [MEM_NUM_512GIG],EAX
|
||||
MOV [MEM_NUM_512GIG+4],EDX
|
||||
|
||||
//Set $LK,"CSysFixedArea",A="MN:CSysFixedArea"$ to zero
|
||||
MOV EDI,SYS_FIXED_AREA
|
||||
XOR EAX,EAX
|
||||
MOV ECX,sizeof(CSysFixedArea)/4
|
||||
REP_STOSD //Init entire SYS_FIXED_AREA to zero
|
||||
REP_STOSD
|
||||
|
||||
MOV EAX,0x80000001
|
||||
CPUID
|
||||
BT EDX,26
|
||||
MOV U32 [MEM_PML2],EDI
|
||||
//Check for 1Gig page capability.
|
||||
BT U32 [MEM_PAGE_SIZE],30
|
||||
JC @@15
|
||||
//Find PML2 Size
|
||||
MOV EAX,U32 [MEM_NUM_2MEG]
|
||||
ADD EAX,0x1FF
|
||||
AND EAX,~0x1FF
|
||||
SHL EAX,3
|
||||
ADD EDI,EAX
|
||||
|
||||
MOV U8 [SYS_GIG_PAGES],FALSE
|
||||
//PML2: Use 2Meg PAges
|
||||
//Find PML3 Size
|
||||
@@15: MOV U32 [MEM_PML3],EDI
|
||||
MOV EAX,U32 [MEM_NUM_1GIG]
|
||||
ADD EAX,0x1FF
|
||||
AND EAX,~0x1FF
|
||||
SHL EAX,3
|
||||
ADD EDI,EAX
|
||||
|
||||
//Find PML4 Size
|
||||
MOV U32 [MEM_PML4],EDI
|
||||
MOV EAX,U32 [MEM_NUM_512GIG]
|
||||
ADD EAX,0x1FF
|
||||
AND EAX,~0x1FF
|
||||
SHL EAX,3
|
||||
ADD EAX,EDI
|
||||
|
||||
MOV U32 [MEM_HEAP_BASE],EAX
|
||||
|
||||
//Set page tables to zero
|
||||
MOV EDI,U32 [MEM_PML2]
|
||||
SUB EAX,EDI
|
||||
MOV ECX,EAX
|
||||
SHR ECX,2
|
||||
XOR EAX,EAX
|
||||
REP_STOSD
|
||||
|
||||
//Check for 1Gig page capability.
|
||||
BT U32 [MEM_PAGE_SIZE],30
|
||||
JC @@30
|
||||
|
||||
//PML2: Use 2Meg Pages
|
||||
MOV EAX,0x87 //bit 7 is page size (2Meg)
|
||||
XOR EDX,EDX
|
||||
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml2
|
||||
MOV ECX,MEM_NUM_2MEG
|
||||
@@05: MOV U32 [EDI],EAX
|
||||
MOV EDI,[MEM_PML2]
|
||||
MOV ECX,[MEM_NUM_2MEG]
|
||||
@@20: MOV U32 [EDI],EAX
|
||||
ADD EDI,4
|
||||
MOV U32 [EDI],EDX
|
||||
ADD EDI,4
|
||||
ADD EAX,0x200000
|
||||
ADC EDX,0
|
||||
LOOP @@05
|
||||
LOOP @@20
|
||||
//PML3: Use 2Meg Pages
|
||||
MOV EAX,SYS_FIXED_AREA+CSysFixedArea.pml2+7
|
||||
MOV EAX,[MEM_PML2]
|
||||
OR EAX,7
|
||||
XOR EDX,EDX
|
||||
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml3
|
||||
MOV ECX,MEM_NUM_USED_1GIG
|
||||
@@10: MOV U32 [EDI],EAX
|
||||
MOV EDI,[MEM_PML3]
|
||||
MOV ECX,[MEM_NUM_1GIG]
|
||||
@@25: MOV U32 [EDI],EAX
|
||||
ADD EDI,4
|
||||
MOV U32 [EDI],EDX
|
||||
ADD EDI,4
|
||||
ADD EAX,0x1000
|
||||
ADC EDX,0
|
||||
LOOP @@10
|
||||
JMP @@25
|
||||
LOOP @@25
|
||||
JMP @@40
|
||||
|
||||
@@15: MOV U8 [SYS_GIG_PAGES],TRUE
|
||||
//PML3: Use 1Gig Pages
|
||||
MOV EAX,0x87 //bit 7 is page size (1Gig)
|
||||
@@30: MOV EAX,0x87 //bit 7 is page size (1Gig)
|
||||
XOR EDX,EDX
|
||||
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml3
|
||||
MOV ECX,MEM_NUM_USED_1GIG
|
||||
@@20: MOV U32 [EDI],EAX
|
||||
MOV EDI,[MEM_PML3]
|
||||
MOV ECX,[MEM_NUM_1GIG]
|
||||
@@35: MOV U32 [EDI],EAX
|
||||
ADD EDI,4
|
||||
MOV U32 [EDI],EDX
|
||||
ADD EDI,4
|
||||
ADD EAX,0x40000000
|
||||
ADC EDX,0
|
||||
LOOP @@20
|
||||
LOOP @@35
|
||||
|
||||
//PML4
|
||||
@@25: MOV EAX,SYS_FIXED_AREA+CSysFixedArea.pml3+7
|
||||
@@40: MOV EAX,[MEM_PML3]
|
||||
OR EAX,7
|
||||
XOR EDX,EDX
|
||||
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml4
|
||||
MOV ECX,MEM_NUM_1GIG/(0x1000/8)
|
||||
@@30: MOV U32 [EDI],EAX
|
||||
MOV EDI,[MEM_PML4]
|
||||
MOV ECX,[MEM_NUM_512GIG]
|
||||
@@45: MOV U32 [EDI],EAX
|
||||
ADD EDI,4
|
||||
MOV U32 [EDI],EDX
|
||||
ADD EDI,4
|
||||
ADD EAX,0x1000
|
||||
ADC EDX,0
|
||||
LOOP @@30
|
||||
LOOP @@45
|
||||
RET
|
||||
|
||||
MOV U32 [SYS_CODE_BP],SYS_FIXED_AREA+CSysFixedArea.adam_bp
|
||||
SYS_INIT_16MEG_SYS_CODE_BP::
|
||||
// Init sys_code_bp to BIOS E801 lowest 16Meg val.
|
||||
// $LK,"BlkPoolsInit",A="MN:BlkPoolsInit"$() adds the rest.
|
||||
MOV U32 [SYS_CODE_BP],SYS_FIXED_AREA+CSysFixedArea.sys_code_bp
|
||||
MOV U32 [SYS_CODE_BP+4],0
|
||||
|
||||
MOV U32 [SYS_DATA_BP],0
|
||||
MOV U32 [SYS_DATA_BP+4],0
|
||||
|
||||
XOR EAX,EAX
|
||||
MOV AX,U16 [SYS_MEM_E801] //1 Kb blks between 1M and 16M
|
||||
MOV AX,U16 [MEM_E801] //1 Kb blks between 1M and 16M
|
||||
SHL EAX,10
|
||||
ADD EAX,0x100000
|
||||
MOV EBX,U32 [SYS_HEAP_BASE] //SYS_FIXED_AREA+sizeof(CSysFixedArea)
|
||||
SUB EAX,EBX
|
||||
MOV EDI,U32 [MEM_HEAP_BASE]
|
||||
SUB EAX,EDI
|
||||
|
||||
/*$AN,"ExpandHeap",A="ExpandHeap"$
|
||||
TempleOS initializes the heap to the val
|
||||
of the BIOS E801 lowest 16Meg val. It
|
||||
later adds the rest to the heap. There
|
||||
are a few allocations which occur while
|
||||
it is set to this small val.
|
||||
|
||||
At some point, it might be nice to
|
||||
initialize to the full val but it is a
|
||||
pain without MAlloc up-and-running.
|
||||
*/
|
||||
|
||||
#define CONSERVATIVE_16MEG_SIZE 0xF00000
|
||||
#define ESTIMATED_MISC_ALLOCS 0x100000
|
||||
|
||||
#assert CONSERVATIVE_16MEG_SIZE-
|
||||
(SYS_FIXED_AREA+sizeof(CSysFixedArea))>MEM_ADAM_STK+
|
||||
ESTIMATED_MISC_ALLOCS
|
||||
|
||||
//EBX=BASE EAX=SIZE
|
||||
//EDI=BASE EAX=SIZE
|
||||
TEST U8 [SYS_MEM_INIT_FLAG],1
|
||||
JZ @@35
|
||||
JZ @@05
|
||||
PUSH EAX
|
||||
MOV EDI,EBX
|
||||
PUSH EDI
|
||||
MOV ECX,EAX
|
||||
MOV AL,U8 [SYS_MEM_INIT_VAL]
|
||||
REP_STOSB
|
||||
POP EDI
|
||||
POP EAX
|
||||
|
||||
@@35: SHR EAX,MEM_PAGE_BITS
|
||||
MOV ESI,SYS_FIXED_AREA+CSysFixedArea.adam_bp
|
||||
MOV EDI,U32 CBlkPool.mem_free_lst[ESI]
|
||||
MOV U32 CMemBlk.next[EBX],EDI
|
||||
MOV U32 CMemBlk.next+4[EBX],0
|
||||
MOV U32 CBlkPool.mem_free_lst[ESI],EBX
|
||||
@@05: SHR EAX,MEM_PAG_BITS
|
||||
MOV ESI,SYS_FIXED_AREA+CSysFixedArea.sys_code_bp
|
||||
MOV EBX,U32 CBlkPool.mem_free_lst[ESI]
|
||||
MOV U32 CMemBlk.next[EDI],EBX
|
||||
MOV U32 CMemBlk.next+4[EDI],0
|
||||
MOV U32 CBlkPool.mem_free_lst[ESI],EDI
|
||||
MOV U32 CBlkPool.mem_free_lst+4[ESI],0
|
||||
MOV U32 CMemBlk.mb_signature[EBX],MBS_UNUSED_SIGNATURE_VAL
|
||||
MOV U32 CMemBlk.pages[EBX],EAX
|
||||
SHL EAX,MEM_PAGE_BITS
|
||||
MOV U32 CMemBlk.mb_signature[EDI],MBS_UNUSED_SIGNATURE_VAL
|
||||
MOV U32 CMemBlk.pags[EDI],EAX
|
||||
SHL EAX,MEM_PAG_BITS
|
||||
ADD U32 CBlkPool.alloced_u8s[ESI],EAX
|
||||
|
||||
BTS U32 [SYS_RUN_LEVEL],RLf_16MEG_SYS_CODE_BP
|
||||
RET
|
||||
}
|
||||
|
||||
I64 *MemPageTable(U8 *a)
|
||||
{//Point to page table entry for addr.
|
||||
if (sys_gig_pages)
|
||||
return SYS_FIXED_AREA+offset(CSysFixedArea.pml3)+a>>30*8;
|
||||
if (Bt(&mem_page_size,30))
|
||||
return *MEM_PML3(U64 *)+a>>30*8;
|
||||
else
|
||||
return SYS_FIXED_AREA+offset(CSysFixedArea.pml2)+a>>21*8;
|
||||
return *MEM_PML2(U64 *)+a>>21*8;
|
||||
}
|
||||
|
||||
I64 MemPageSize(U8 *)
|
||||
{//Page table entry size for addr.
|
||||
if (sys_gig_pages)
|
||||
return 1<<30; //1Gig
|
||||
else
|
||||
return 1<<21; //2Meg
|
||||
}
|
||||
|
||||
Bool MemPagePresentMark(U8 *a,Bool val)
|
||||
{//Mark an addr's page present or not-present.
|
||||
I64 *pte=MemPageTable(a);
|
||||
Bool res=*pte&1;
|
||||
if (val)
|
||||
*pte=*pte|1; //Mark present
|
||||
else
|
||||
*pte=*pte&~1; //Mark not present
|
||||
InvlPg(a);
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user