git.ucc.asn.au
/
tpg
/
acess2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Linking and MM Fiddling (mm now handles large pages - well, partially)
[tpg/acess2.git]
/
Kernel
/
arch
/
x86_64
/
mm_phys.c
diff --git
a/Kernel/arch/x86_64/mm_phys.c
b/Kernel/arch/x86_64/mm_phys.c
index
e2c2cf6
..
55fc68a
100644
(file)
--- a/
Kernel/arch/x86_64/mm_phys.c
+++ b/
Kernel/arch/x86_64/mm_phys.c
@@
-3,6
+3,7
@@
*
* Physical Memory Manager
*/
*
* Physical Memory Manager
*/
+#define DEBUG 0
#include <acess.h>
#include <mboot.h>
#include <mm_virt.h>
#include <acess.h>
#include <mboot.h>
#include <mm_virt.h>
@@
-18,8
+19,8
@@
enum eMMPhys_Ranges
};
// === IMPORTS ===
};
// === IMPORTS ===
-extern
void gKernelBase
;
-extern
void gKernelEnd
;
+extern
char gKernelBase[]
;
+extern
char gKernelEnd[]
;
// === PROTOTYPES ===
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
// === PROTOTYPES ===
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
@@
-31,9
+32,9
@@
void MM_DerefPhys(tPAddr PAddr);
// === GLOBALS ===
tSpinlock glPhysicalPages;
// === GLOBALS ===
tSpinlock glPhysicalPages;
-Uint64 *gaSuperBitmap; // 1 bit = 64 Pages, 16 MiB Per Word
-Uint64 *gaMainBitmap; // 1 bit = 1 Page, 256 KiB per Word
-Uint64 *gaMultiBitmap; // Each bit means that the page is being used multiple times
+Uint64 *gaSuperBitmap
= (void*)MM_PAGE_SUPBMP
; // 1 bit = 64 Pages, 16 MiB Per Word
+Uint64 *gaMainBitmap
= (void*)MM_PAGE_BITMAP
; // 1 bit = 1 Page, 256 KiB per Word
+Uint64 *gaMultiBitmap
= (void*)MM_PAGE_DBLBMP
; // Each bit means that the page is being used multiple times
Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS; // Reference Counts
tPAddr giFirstFreePage; // First possibly free page
Uint64 giPhysRangeFree[NUM_MM_PHYS_RANGES]; // Number of free pages in each range
Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS; // Reference Counts
tPAddr giFirstFreePage; // First possibly free page
Uint64 giPhysRangeFree[NUM_MM_PHYS_RANGES]; // Number of free pages in each range
@@
-61,18
+62,18
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
tVAddr vaddr;
tPAddr paddr, firstFreePage;
tVAddr vaddr;
tPAddr paddr, firstFreePage;
-
Log("MM_InitPhys_Multiboot: (MBoot=%p)
", MBoot);
+
ENTER("pMBoot=%p
", MBoot);
// Scan the physical memory map
// Looking for the top of physical memory
mmapStart = (void *)( KERNEL_BASE | MBoot->MMapAddr );
// Scan the physical memory map
// Looking for the top of physical memory
mmapStart = (void *)( KERNEL_BASE | MBoot->MMapAddr );
- L
og(" MM_InitPhys_Multiboot:
mmapStart = %p", mmapStart);
+ L
OG("
mmapStart = %p", mmapStart);
ent = mmapStart;
while( (Uint)ent < (Uint)mmapStart + MBoot->MMapLength )
{
// Adjust for the size of the entry
ent->Size += 4;
ent = mmapStart;
while( (Uint)ent < (Uint)mmapStart + MBoot->MMapLength )
{
// Adjust for the size of the entry
ent->Size += 4;
- L
og(" MM_InitPhys_Multiboot:
ent={Type:%i,Base:0x%x,Length:%x",
+ L
OG("
ent={Type:%i,Base:0x%x,Length:%x",
ent->Type, ent->Base, ent->Length);
// If entry is RAM and is above `maxAddr`, change `maxAddr`
ent->Type, ent->Base, ent->Length);
// If entry is RAM and is above `maxAddr`, change `maxAddr`
@@
-92,7
+93,7
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
// Goodie, goodie gumdrops
giMaxPhysPage = maxAddr >> 12;
}
// Goodie, goodie gumdrops
giMaxPhysPage = maxAddr >> 12;
}
- L
og(" MM_InitPhys_Multiboot:
giMaxPhysPage = 0x%x", giMaxPhysPage);
+ L
OG("
giMaxPhysPage = 0x%x", giMaxPhysPage);
// Find a contigous section of memory to hold it in
// - Starting from the end of the kernel
// Find a contigous section of memory to hold it in
// - Starting from the end of the kernel
@@
-100,8
+101,7
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
superPages = ((giMaxPhysPage+64*8-1)/(64*8) + 0xFFF) >> 12;
numPages = (giMaxPhysPage + 7) / 8;
numPages = (numPages + 0xFFF) >> 12;
superPages = ((giMaxPhysPage+64*8-1)/(64*8) + 0xFFF) >> 12;
numPages = (giMaxPhysPage + 7) / 8;
numPages = (numPages + 0xFFF) >> 12;
- Log(" MM_InitPhys_Multiboot: numPages = %i, superPages = %i",
- numPages, superPages);
+ LOG("numPages = %i, superPages = %i", numPages, superPages);
if(maxAddr == 0)
{
int todo = numPages*2 + superPages;
if(maxAddr == 0)
{
int todo = numPages*2 + superPages;
@@
-153,7
+153,7
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
if( ent->Base + ent->Size < (tPAddr)&gKernelBase )
continue;
if( ent->Base + ent->Size < (tPAddr)&gKernelBase )
continue;
- L
og(" MM_InitPhys_Multiboot:
%x <= %x && %x > %x",
+ L
OG("
%x <= %x && %x > %x",
ent->Base, (tPAddr)&gKernelBase,
ent->Base + ent->Size, (tPAddr)&gKernelEnd - KERNEL_BASE
);
ent->Base, (tPAddr)&gKernelBase,
ent->Base + ent->Size, (tPAddr)&gKernelEnd - KERNEL_BASE
);
@@
-206,14
+206,15
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
// Save the current value of paddr to simplify the allocation later
firstFreePage = paddr;
// Save the current value of paddr to simplify the allocation later
firstFreePage = paddr;
- L
og(" MM_InitPhys_Multiboot:
Clearing multi bitmap");
+ L
OG("
Clearing multi bitmap");
// Fill the bitmaps
// Fill the bitmaps
- memset(gaMultiBitmap, 0,
numPages<<12
);
+ memset(gaMultiBitmap, 0,
(numPages<<12)/8
);
// - initialise to one, then clear the avaliable areas
// - initialise to one, then clear the avaliable areas
- memset(gaMainBitmap, -1, numPages<<12);
- Log(" MM_InitPhys_Multiboot: Setting main bitmap");
+ memset(gaMainBitmap, -1, (numPages<<12)/8);
+ memset(gaSuperBitmap, -1, (numPages<<12)/(8*64));
+ LOG("Setting main bitmap");
// - Clear all Type=1 areas
// - Clear all Type=1 areas
- L
og(" MM_InitPhys_Multiboot:
Clearing valid regions");
+ L
OG("
Clearing valid regions");
for(
ent = mmapStart;
(Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
for(
ent = mmapStart;
(Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
@@
-228,14
+229,14
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
size = ent->Size >> 12;
if(base & 63) {
size = ent->Size >> 12;
if(base & 63) {
- Uint64 val = -1 << (base & 63);
+ Uint64 val = -1
LL
<< (base & 63);
gaMainBitmap[base / 64] &= ~val;
size -= (base & 63);
base += 64 - (base & 63);
}
memset( &gaMainBitmap[base / 64], 0, size/8 );
if( size & 7 ) {
gaMainBitmap[base / 64] &= ~val;
size -= (base & 63);
base += 64 - (base & 63);
}
memset( &gaMainBitmap[base / 64], 0, size/8 );
if( size & 7 ) {
- Uint64 val = -1 << (size & 7);
+ Uint64 val = -1
LL
<< (size & 7);
val <<= (size/8)&7;
gaMainBitmap[base / 64] &= ~val;
}
val <<= (size/8)&7;
gaMainBitmap[base / 64] &= ~val;
}
@@
-246,7
+247,7
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
size = (size + (base & 63) + 63) >> 6;
base = base >> 6;
if(base & 63) {
size = (size + (base & 63) + 63) >> 6;
base = base >> 6;
if(base & 63) {
- Uint64 val = -1 << (base & 63);
+ Uint64 val = -1
LL
<< (base & 63);
gaSuperBitmap[base / 64] &= ~val;
size -= (base & 63);
base += 64 - (base & 63);
gaSuperBitmap[base / 64] &= ~val;
size -= (base & 63);
base += 64 - (base & 63);
@@
-258,7
+259,7
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
size = firstFreePage >> 12;
memset( &gaMainBitmap[base / 64], -1, size/8 );
if( size & 7 ) {
size = firstFreePage >> 12;
memset( &gaMainBitmap[base / 64], -1, size/8 );
if( size & 7 ) {
- Uint64 val = -1 << (size & 7);
+ Uint64 val = -1
LL
<< (size & 7);
val <<= (size/8)&7;
gaMainBitmap[base / 64] |= val;
}
val <<= (size/8)&7;
gaMainBitmap[base / 64] |= val;
}
@@
-268,16
+269,16
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
if(gaiStaticAllocPages[i] != 0)
continue;
gaMainBitmap[ gaiStaticAllocPages[i] >> (12+6) ]
if(gaiStaticAllocPages[i] != 0)
continue;
gaMainBitmap[ gaiStaticAllocPages[i] >> (12+6) ]
- &= ~(1 << ((gaiStaticAllocPages[i]>>12)&63));
+ &= ~(1
LL
<< ((gaiStaticAllocPages[i]>>12)&63));
}
// Fill the super bitmap
}
// Fill the super bitmap
- L
og(" MM_InitPhys_Multiboot:
Filling super bitmap");
+ L
OG("
Filling super bitmap");
memset(gaSuperBitmap, 0, superPages<<12);
for( base = 0; base < (size+63)/64; base ++)
{
memset(gaSuperBitmap, 0, superPages<<12);
for( base = 0; base < (size+63)/64; base ++)
{
- if( gaMainBitmap[ base ]
== -1
)
- gaSuperBitmap[ base/64 ] |= 1 << (base&63);
+ if( gaMainBitmap[ base ]
+ 1 == 0
)
+ gaSuperBitmap[ base/64 ] |= 1
LL
<< (base&63);
}
// Set free page counts
}
// Set free page counts
@@
-285,7
+286,7
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
{
int rangeID;
// Skip allocated
{
int rangeID;
// Skip allocated
- if( gaMainBitmap[ base >> 6 ] & (1
<< (base&63)) )
continue;
+ if( gaMainBitmap[ base >> 6 ] & (1
LL << (base&63)) )
continue;
// Get range ID
rangeID = MM_int_GetRangeID( base << 12 );
// Get range ID
rangeID = MM_int_GetRangeID( base << 12 );
@@
-300,6
+301,8
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
// updated anymore, hence will be correct)
giPhysRangeLast[ rangeID ] = base;
}
// updated anymore, hence will be correct)
giPhysRangeLast[ rangeID ] = base;
}
+
+ LEAVE('-');
}
/**
}
/**
@@
-312,27
+315,26
@@
void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
*/
tPAddr MM_AllocPhysRange(int Num, int Bits)
{
*/
tPAddr MM_AllocPhysRange(int Num, int Bits)
{
- tPAddr addr;
+ tPAddr addr
, ret
;
int rangeID;
int nFree = 0, i;
int rangeID;
int nFree = 0, i;
-
Log("MM_AllocPhysRange: (Num=%i,Bits=%i)
", Num, Bits);
+
ENTER("iNum iBits
", Num, Bits);
if( Bits <= 0 || Bits >= 64 ) // Speedup for the common case
rangeID = MM_PHYS_MAX;
else
if( Bits <= 0 || Bits >= 64 ) // Speedup for the common case
rangeID = MM_PHYS_MAX;
else
- rangeID = MM_int_GetRangeID( (1
<< Bits) -
1 );
+ rangeID = MM_int_GetRangeID( (1
LL << Bits) -
1 );
- L
og(" MM_AllocPhysRange:
rangeID = %i", rangeID);
+ L
OG("
rangeID = %i", rangeID);
LOCK(&glPhysicalPages);
LOCK(&glPhysicalPages);
- Log(" MM_AllocPhysRange: i has lock");
// Check if the range actually has any free pages
while(giPhysRangeFree[rangeID] == 0 && rangeID)
rangeID --;
// Check if the range actually has any free pages
while(giPhysRangeFree[rangeID] == 0 && rangeID)
rangeID --;
- L
og(" MM_AllocPhysRange:
rangeID = %i", rangeID);
+ L
OG("
rangeID = %i", rangeID);
// What the? Oh, man. No free pages
if(giPhysRangeFree[rangeID] == 0) {
// What the? Oh, man. No free pages
if(giPhysRangeFree[rangeID] == 0) {
@@
-344,13
+346,14
@@
tPAddr MM_AllocPhysRange(int Num, int Bits)
"Out of memory (unable to fulfil request for %i pages), zero remaining",
Num
);
"Out of memory (unable to fulfil request for %i pages), zero remaining",
Num
);
+ LEAVE('i', 0);
return 0;
}
// Check if there is enough in the range
if(giPhysRangeFree[rangeID] >= Num)
{
return 0;
}
// Check if there is enough in the range
if(giPhysRangeFree[rangeID] >= Num)
{
- L
og(" MM_AllocPhysRange:
{%i,0x%x -> 0x%x}",
+ L
OG("
{%i,0x%x -> 0x%x}",
giPhysRangeFree[rangeID],
giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
);
giPhysRangeFree[rangeID],
giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
);
@@
-362,35
+365,35
@@
tPAddr MM_AllocPhysRange(int Num, int Bits)
{
//Log(" MM_AllocPhysRange: addr = 0x%x", addr);
// Check the super bitmap
{
//Log(" MM_AllocPhysRange: addr = 0x%x", addr);
// Check the super bitmap
- if( gaSuperBitmap[addr >> (6+6)]
== -1
) {
- L
og(" MM_AllocPhysRange:
nFree = %i = 0 (super) (0x%x)", nFree, addr);
+ if( gaSuperBitmap[addr >> (6+6)]
+ 1 == 0
) {
+ L
OG("
nFree = %i = 0 (super) (0x%x)", nFree, addr);
nFree = 0;
nFree = 0;
- addr += 1 << (6+6);
- addr &=
(1 << (6+6)) - 1;
+ addr += 1
LL
<< (6+6);
+ addr &=
~0xFFF; // (1LL << 6+6) - 1
continue;
}
// Check page block (64 pages)
continue;
}
// Check page block (64 pages)
- if( ga
SuperBitmap[addr >> (6+6)] & (1 << (addr>>6)&63)
) {
- L
og(" MM_AllocPhysRange:
nFree = %i = 0 (main) (0x%x)", nFree, addr);
+ if( ga
MainBitmap[addr >> 6] + 1 == 0
) {
+ L
OG("
nFree = %i = 0 (main) (0x%x)", nFree, addr);
nFree = 0;
nFree = 0;
- addr += 1
<< (12+
6);
- addr &=
(1 << (12+6)) - 1
;
+ addr += 1
LL << (
6);
+ addr &=
~0x3F
;
continue;
}
// Check individual page
continue;
}
// Check individual page
- if( gaMainBitmap[addr >> 6] & (1 << (addr & 63)) ) {
- L
og(" MM_AllocPhysRange:
nFree = %i = 0 (page) (0x%x)", nFree, addr);
+ if( gaMainBitmap[addr >> 6] & (1
LL
<< (addr & 63)) ) {
+ L
OG("
nFree = %i = 0 (page) (0x%x)", nFree, addr);
nFree = 0;
addr ++;
continue;
}
nFree ++;
addr ++;
nFree = 0;
addr ++;
continue;
}
nFree ++;
addr ++;
- L
og(" MM_AllocPhysRange:
nFree(%i) == %i (0x%x)", nFree, Num, addr);
+ L
OG("
nFree(%i) == %i (0x%x)", nFree, Num, addr);
if(nFree == Num)
break;
}
if(nFree == Num)
break;
}
- L
og(" MM_AllocPhysRange:
nFree = %i", nFree);
+ L
OG("
nFree = %i", nFree);
// If we don't find a contiguous block, nFree will not be equal
// to Num, so we set it to zero and do the expensive lookup.
if(nFree != Num) nFree = 0;
// If we don't find a contiguous block, nFree will not be equal
// to Num, so we set it to zero and do the expensive lookup.
if(nFree != Num) nFree = 0;
@@
-411,18
+414,23
@@
tPAddr MM_AllocPhysRange(int Num, int Bits)
"Out of memory (unable to fulfil request for %i pages)",
Num
);
"Out of memory (unable to fulfil request for %i pages)",
Num
);
+ LEAVE('i', 0);
return 0;
}
return 0;
}
- L
og(" MM_AllocPhysRange:
nFree = %i, addr = 0x%08x", nFree, addr);
+ L
OG("
nFree = %i, addr = 0x%08x", nFree, addr);
// Mark pages as allocated
addr -= Num;
// Mark pages as allocated
addr -= Num;
- for( i = 0; i < Num; i++ )
+ for( i = 0; i < Num; i++
, addr++
)
{
{
- gaMainBitmap[addr >> 6] |= 1 << (addr & 63);
- rangeID = MM_int_GetRangeID(addr);
+ gaMainBitmap[addr >> 6] |= 1
LL
<< (addr & 63);
+ rangeID = MM_int_GetRangeID(addr
<< 12
);
giPhysRangeFree[ rangeID ] --;
giPhysRangeFree[ rangeID ] --;
+ LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
+ if(addr == giPhysRangeFirst[ rangeID ])
+ giPhysRangeFirst[ rangeID ] += 1;
}
}
+ ret = addr; // Save the return address
// Update super bitmap
Num += addr & (64-1);
// Update super bitmap
Num += addr & (64-1);
@@
-430,13
+438,13
@@
tPAddr MM_AllocPhysRange(int Num, int Bits)
Num = (Num + (64-1)) & ~(64-1);
for( i = 0; i < Num/64; i++ )
{
Num = (Num + (64-1)) & ~(64-1);
for( i = 0; i < Num/64; i++ )
{
- if( gaMainBitmap[ addr >> 6 ]
== -1
)
- gaSuperBitmap[addr>>12] |= 1
<< ((addr >> 6) & 64
);
+ if( gaMainBitmap[ addr >> 6 ]
+ 1 == 0
)
+ gaSuperBitmap[addr>>12] |= 1
LL << ((addr >> 6) & 63
);
}
RELEASE(&glPhysicalPages);
}
RELEASE(&glPhysicalPages);
- L
og("MM_AllocPhysRange: RETURN %x", addr
<< 12);
- return
addr
<< 12;
+ L
EAVE('x', ret
<< 12);
+ return
ret
<< 12;
}
/**
}
/**
@@
-469,18
+477,18
@@
void MM_RefPhys(tPAddr PAddr)
if( PAddr >> 12 > giMaxPhysPage ) return ;
if( PAddr >> 12 > giMaxPhysPage ) return ;
- if( gaMainBitmap[ page >> 6 ] & (1 << (page&63)) )
+ if( gaMainBitmap[ page >> 6 ] & (1
LL
<< (page&63)) )
{
// Reference again
{
// Reference again
- gaMultiBitmap[ page >> 6 ] |= 1 << (page&63);
+ gaMultiBitmap[ page >> 6 ] |= 1
LL
<< (page&63);
gaiPageReferences[ page ] ++;
}
else
{
// Allocate
gaiPageReferences[ page ] ++;
}
else
{
// Allocate
- gaMainBitmap[page >> 6] |= 1 << (page&63);
- if( gaMainBitmap[page >> 6 ]
== -1
)
- gaSuperBitmap[page>> 12] |= 1 << ((page >> 6) & 63);
+ gaMainBitmap[page >> 6] |= 1
LL
<< (page&63);
+ if( gaMainBitmap[page >> 6 ]
+ 1 == 0
)
+ gaSuperBitmap[page>> 12] |= 1
LL
<< ((page >> 6) & 63);
}
}
}
}
@@
-493,26
+501,31
@@
void MM_DerefPhys(tPAddr PAddr)
if( PAddr >> 12 > giMaxPhysPage ) return ;
if( PAddr >> 12 > giMaxPhysPage ) return ;
- if( gaMultiBitmap[ page >> 6 ] & (1 << (page&63)) ) {
+ if( gaMultiBitmap[ page >> 6 ] & (1
LL
<< (page&63)) ) {
gaiPageReferences[ page ] --;
if( gaiPageReferences[ page ] == 1 )
gaiPageReferences[ page ] --;
if( gaiPageReferences[ page ] == 1 )
- gaMultiBitmap[ page >> 6 ] &= ~(1 << (page&63));
+ gaMultiBitmap[ page >> 6 ] &= ~(1
LL
<< (page&63));
if( gaiPageReferences[ page ] == 0 )
if( gaiPageReferences[ page ] == 0 )
- gaMainBitmap[ page >> 6 ] &= ~(1 << (page&63));
+ gaMainBitmap[ page >> 6 ] &= ~(1
LL
<< (page&63));
}
else
}
else
- gaMainBitmap[ page >> 6 ] &= ~(1 << (page&63));
+ gaMainBitmap[ page >> 6 ] &= ~(1
LL
<< (page&63));
- //
TODO: Update free counts
- if( !(gaMainBitmap[ page >> 6 ] & (1 << (page&63))) )
+ //
Update the free counts if the page was freed
+ if( !(gaMainBitmap[ page >> 6 ] & (1
LL
<< (page&63))) )
{
int rangeID;
rangeID = MM_int_GetRangeID( PAddr );
giPhysRangeFree[ rangeID ] ++;
{
int rangeID;
rangeID = MM_int_GetRangeID( PAddr );
giPhysRangeFree[ rangeID ] ++;
+ if( giPhysRangeFirst[rangeID] > page )
+ giPhysRangeFirst[rangeID] = page;
+ if( giPhysRangeLast[rangeID] < page )
+ giPhysRangeLast[rangeID] = page;
}
}
- if(gaMainBitmap[ page >> 6 ] == 0) {
- gaSuperBitmap[page >> 12] &= ~(1 << ((page >> 6) & 63));
+ // If the bitmap entry is not -1, unset the bit in the super bitmap
+ if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
+ gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
}
}
}
}
UCC
git Repository :: git.ucc.asn.au