TempleOSCDV4.06.ISO

This commit is contained in:
Terry A. Davis authored and minexew committed 2018-09-08 21:47:19 +02:00
1 parent 6fe38d2d9d
commit b632ab9a4f
261 files changed
+6282 -5801

No files matched your search

+25 -25
View File
@@ -145,7 +145,7 @@ _MALLOC::
@@75: ADD RSI,U64 CMemUnused.size[RSI]
MALLOC_ALMOST_DONE:
//RSI=result-CMemUsed.size
//RSI=res-CMemUsed.size
//RAX=size+CMemUsed.size
//RDX=HeapCtrl
ADD U64 CHeapCtrl.used_u8s[RDX],RAX
@@ -394,9 +394,9 @@ U8 *AMAlloc(I64 size)
U8 *CAlloc(I64 size,CTask *mem_task=NULL)
{//Accepts a $LK,"CTask",A="MN:CTask"$ or $LK,"CHeapCtrl",A="MN:CHeapCtrl"$. NULL allocs off current task's heap.
U8 *result=MAlloc(size,mem_task);
MemSet(result,0,size);
return result;
U8 *res=MAlloc(size,mem_task);
MemSet(res,0,size);
return res;
}
U8 *ACAlloc(I64 size)
@@ -404,20 +404,20 @@ U8 *ACAlloc(I64 size)
return CAlloc(size,adam_task);
}
U8 *MAllocIdentical(U8 *src,CTask *mem_task=NULL)
U8 *MAllocIdent(U8 *src,CTask *mem_task=NULL)
{//Accepts a $LK,"CTask",A="MN:CTask"$ or $LK,"CHeapCtrl",A="MN:CHeapCtrl"$. NULL allocs off current task's heap.
U8 *result;
U8 *res;
I64 size;
if (!src) return NULL;
size=MSize(src);
result=MAlloc(size,mem_task);
MemCpy(result,src,size);
return result;
res=MAlloc(size,mem_task);
MemCpy(res,src,size);
return res;
}
U8 *AMAllocIdentical(U8 *src)
{//Alloc in Adam's heap, identical copy of heap node.
return MAllocIdentical(src,adam_task);
U8 *AMAllocIdent(U8 *src)
{//Alloc in Adam's heap, ident copy of heap node.
return MAllocIdent(src,adam_task);
}
U8 *MAllocAligned(I64 size,I64 alignment,
@@ -425,10 +425,10 @@ U8 *MAllocAligned(I64 size,I64 alignment,
{//Only powers of two alignment. This is awful.
I64 mask=alignment-1;
U8 *ptr=MAlloc(size+mask+sizeof(I64)+misalignment,mem_task),
*result=(ptr+sizeof(I64)+mask)&~mask+misalignment;
result(I64 *)[-1]=ptr-result;
*res=(ptr+sizeof(I64)+mask)&~mask+misalignment;
res(I64 *)[-1]=ptr-res;
#assert offset(CMemUsed.size)==offset(CMemUsed.start)-sizeof(I64)
return result;
return res;
}
U8 *CAllocAligned(I64 size,I64 alignment,
@@ -436,26 +436,26 @@ U8 *CAllocAligned(I64 size,I64 alignment,
{//Only powers of two alignment. This is awful.
I64 mask=alignment-1;
U8 *ptr=MAlloc(size+mask+sizeof(I64)+misalignment,mem_task),
*result=(ptr+sizeof(I64)+mask)&~mask+misalignment;
result(I64 *)[-1]=ptr-result;
*res=(ptr+sizeof(I64)+mask)&~mask+misalignment;
res(I64 *)[-1]=ptr-res;
#assert offset(CMemUsed.size)==offset(CMemUsed.start)-sizeof(I64)
MemSet(result,0,size);
return result;
MemSet(res,0,size);
return res;
}
U8 *StrNew(U8 *buf,CTask *mem_task=NULL)
{//Accepts a $LK,"CTask",A="MN:CTask"$ or $LK,"CHeapCtrl",A="MN:CHeapCtrl"$. NULL allocs off current task's heap.
U8 *result;
U8 *res;
I64 size;
if (buf) {
size=StrLen(buf)+1;
result=MAlloc(size,mem_task);
MemCpy(result,buf,size);
res=MAlloc(size,mem_task);
MemCpy(res,buf,size);
} else {
result=MAlloc(1,mem_task);
*result=0;
res=MAlloc(1,mem_task);
*res=0;
}
return result;
return res;
}
U8 *AStrNew(U8 *buf)
+23 -23
View File
@@ -16,7 +16,7 @@ so a ptr to a page count is passed.
Return: NULL if out of memory.
*/
CMemBlk *result=NULL,*m;
CMemBlk *res=NULL,*m;
I64 i;
if (!bp) bp=sys_code_bp;
PUSHFD
@@ -24,8 +24,8 @@ Return: NULL if out of memory.
while (LBts(&bp->locked_flags,BPlf_LOCKED))
PAUSE
if (pages512<MEM_FREE_PAGE_HASH_SIZE) {
if (result=bp->free_page_hash[pages512]) {
bp->free_page_hash[pages512]=result->next;
if (res=bp->free_page_hash[pages512]) {
bp->free_page_hash[pages512]=res->next;
goto at_done;
}
i=Bsr(MEM_FREE_PAGE_HASH_SIZE)+1;
@@ -38,37 +38,37 @@ Return: NULL if out of memory.
pages512-=MEM_EXTRA_HASH2_PAGES;
i=Bsr(pages512)+1;
pages512=1<<i+MEM_EXTRA_HASH2_PAGES;
if (result=bp->free_page_hash2[i]) {
bp->free_page_hash2[i]=result->next;
if (res=bp->free_page_hash2[i]) {
bp->free_page_hash2[i]=res->next;
goto at_done;
}
}
m=&bp->mem_free_lst;
while (TRUE) {
if (!(result=m->next)) {
if (!(res=m->next)) {
//We're probably out of luck, but lets search for a
//freed larger size block... and, screw-it, return the whole thing.
do {
if (result=bp->free_page_hash2[++i]) {
if (res=bp->free_page_hash2[++i]) {
pages512=1<<i+MEM_EXTRA_HASH2_PAGES;
bp->free_page_hash2[i]=result->next;
bp->free_page_hash2[i]=res->next;
goto at_done;
}
} while (i<64-MEM_PAGE_BITS-1);
pages512=0;
result=NULL; //Out of memory
res=NULL; //Out of memory
goto at_done;
}
if (result->pages<pages512)
m=result;
if (res->pages<pages512)
m=res;
else {
if (result->pages==pages512) {
m->next=result->next;
if (res->pages==pages512) {
m->next=res->next;
goto at_done;
} else {
result->pages-=pages512;
result(U8 *)+=result->pages<<MEM_PAGE_BITS;
result->pages=pages512;
res->pages-=pages512;
res(U8 *)+=res->pages<<MEM_PAGE_BITS;
res->pages=pages512;
goto at_done;
}
}
@@ -77,7 +77,7 @@ at_done:
bp->used_u8s+=pages512<<MEM_PAGE_BITS;
LBtr(&bp->locked_flags,BPlf_LOCKED);
POPFD
return result;
return res;
}
U0 Mem512Free(CMemBlk *m,CBlkPool *bp=NULL)
@@ -116,13 +116,13 @@ CMemBlk *Mem512TaskAlloc(I64 pages512,CHeapCtrl *hc)
Currently, this is only called from $LK,"MAlloc",A="MN:MAlloc"$().
Return: NULL if out of memory.
*/
CMemBlk *result;
CMemBlk *res;
I64 threshold,cnt,size;
CMemUnused *uum,**_uum,**_ptr;
if (result=Mem512Alloc(pages512,hc->bp)) {
QueIns(result,hc->last_mem_blk);
result->mb_signature=MBS_USED_SIGNATURE_VAL;
hc->alloced_u8s+=result->pages<<MEM_PAGE_BITS;
if (res=Mem512Alloc(pages512,hc->bp)) {
QueIns(res,hc->last_mem_blk);
res->mb_signature=MBS_USED_SIGNATURE_VAL;
hc->alloced_u8s+=res->pages<<MEM_PAGE_BITS;
//Tidy-up free lst (Move into heap hash)
//because if free lst gets long, delay causes crash
@@ -147,7 +147,7 @@ Return: NULL if out of memory.
threshold<<=1;
} while (cnt>8 && threshold<=MEM_HEAP_HASH_SIZE);
}
return result;
return res;
}
U0 Mem512TaskFree(CMemBlk *m,CHeapCtrl *hc)
+12 -17
View File
@@ -55,24 +55,11 @@ U0 Mem32DevInit()
}
}
Bool MemLowPagesProtect(Bool val=TRUE)
{//Mark low memory not present. Catch NULL deref.
//Returns: FALSE if any were not protected.
U8 *a=0;
Bool result=TRUE;
while (a<=MEM_PROTECTED_LOW_LIMIT) {
result&=!MemPagePresentMark(a,!val);
a+=MemPageSize(a);
}
return result;
}
U0 MemPagesNotPresentMark()
{
U8 *a,*max_physical=NULL;
U16 *m01=SYS_MEM_E801;
CMemE820 *m20=SYS_MEM_E820;
MemLowPagesProtect;
while (m20->type) {
a=m20->base+m20->len;
if (a>max_physical)
@@ -83,7 +70,7 @@ U0 MemPagesNotPresentMark()
a=(max_physical+0x1FFFFF)&~0x1FFFFF;
while (a<MEM_MAPPED_SPACE) {
MemPagePresentMark(a,FALSE);
a+=MemPageSize(a);
a+=0x200000;
}
}
}
@@ -140,7 +127,7 @@ U0 Mem32DevFree(U8 *base)
U8 *Mem64DevAlloc(I64 *_pages2Meg)
{//Alloc 64-bit addr space for device.
U8 *result;
U8 *res;
I64 i=*_pages2Meg,*pte;
while (LBts(&sys_semas[SYS_SEMA_DEV_MEM],0))
Yield;
@@ -150,9 +137,9 @@ U8 *Mem64DevAlloc(I64 *_pages2Meg)
*pte=*pte&~0x18 |0x11; //Uncached and present
InvlPg(dev.mem64_ptr);
}
result=dev.mem64_ptr;
res=dev.mem64_ptr;
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
return result;
return res;
}
U0 Mem64DevFree(U8 *base,I64 pages2Meg)
@@ -166,6 +153,14 @@ U0 Mem64DevFree(U8 *base,I64 pages2Meg)
LBtr(&sys_semas[SYS_SEMA_DEV_MEM],0);
}
U8 *MemVGAAlloc()
{
I64 i=1,*a=Mem64DevAlloc(&i),*pte=MemPageTable(a);
*pte=0x18F;
InvlPg(a);
return a;
}
I64 BIOSTotalMem()
{
I64 r01,r20;
+6 -53
View File
@@ -2,54 +2,18 @@ asm {
// $LK,"::/Doc/MemOverview.TXT"$
USE32
SYS_INIT_MEM::
SYS_INIT_PAGE_TABLES::
//Set page tables to identity map everything.
MOV EDI,SYS_FIXED_AREA
XOR EAX,EAX
MOV ECX,sizeof(CSysFixedArea)/4
REP_STOSD //Init entire SYS_FIXED_AREA to zero
//PML1
MOV EAX,0x107
XOR EDX,EDX
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml1
MOV ECX,MEM_NUM_PML1*0x200
@@05: MOV U32 [EDI],EAX
ADD EDI,4
MOV U32 [EDI],EDX
ADD EDI,4
ADD EAX,0x1000
ADC EDX,0
LOOP @@05
//0xA0000-0xBFFFF: video ram=write thru
MOV EAX,0xF
XOR EDX,EDX
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml1+0xA0*8
MOV ECX,0xC0-0xA0
@@10: OR U32 [EDI],EAX
ADD EDI,4
OR U32 [EDI],EDX
ADD EDI,4
LOOP @@10
//PML2
MOV EAX,SYS_FIXED_AREA+CSysFixedArea.pml1+7
MOV EAX,0x87 //bit 7 is page size (1==2Meg)
XOR EDX,EDX
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml2
MOV ECX,MEM_NUM_PML1
@@15: MOV U32 [EDI],EAX
ADD EDI,4
MOV U32 [EDI],EDX
ADD EDI,4
ADD EAX,0x1000
ADC EDX,0
LOOP @@15
MOV EAX,MEM_NUM_PML1*0x200000+0x87 //bit 7 is page size (1==2Meg)
XOR EDX,EDX
MOV EDI,SYS_FIXED_AREA+CSysFixedArea.pml2+8*MEM_NUM_PML1
MOV ECX,MEM_NUM_PML2*0x200-MEM_NUM_PML1
MOV ECX,MEM_NUM_PML2*0x200
@@20: MOV U32 [EDI],EAX
ADD EDI,4
MOV U32 [EDI],EDX
@@ -151,31 +115,20 @@ pain without MAlloc up-and-running.
RET
}
I64 MemPageSize(U8 *a)
{//Size of page table at addr.Either 4K or 2Meg.
if (a<MEM_NUM_PML1*0x1000*0x200)
return 0x1000;
else
return 0x200000;
}
I64 *MemPageTable(U8 *a)
{//Point to page table entry for addr.
if (a<MEM_NUM_PML1*0x1000*0x200)
return SYS_FIXED_AREA+offset(CSysFixedArea.pml1)+a>>12*8;
else
return SYS_FIXED_AREA+offset(CSysFixedArea.pml2)+a>>21*8;
return SYS_FIXED_AREA+offset(CSysFixedArea.pml2)+a>>21*8;
}
Bool MemPagePresentMark(U8 *a,Bool val)
{//Mark an addr's page present or not-present.
I64 *pte=MemPageTable(a);
Bool result=*pte&1;
Bool res=*pte&1;
if (val)
*pte=*pte|1; //Mark present
else
*pte=*pte&~1; //Mark not present
InvlPg(a);
return result;
return res;
}