4 * Physical Memory Manager
13 MM_PHYS_16BIT, // Does anything need this?
14 MM_PHYS_20BIT, // Real-Mode
15 MM_PHYS_24BIT, // ISA DMA
16 MM_PHYS_32BIT, // x86 Hardware
17 MM_PHYS_MAX, // Doesn't care
22 extern void gKernelBase;
23 extern void gKernelEnd;
26 void MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
27 tPAddr MM_AllocPhysRange(int Num, int Bits);
28 tPAddr MM_AllocPhys(void);
29 void MM_RefPhys(tPAddr PAddr);
30 void MM_DerefPhys(tPAddr PAddr);
31 int MM_int_GetRangeID( tPAddr Addr );
34 tSpinlock glPhysicalPages;
35 Uint64 *gaSuperBitmap; // 1 bit = 64 Pages, 16 MiB Per Word
36 Uint64 *gaMainBitmap; // 1 bit = 1 Page, 256 KiB per Word
37 Uint64 *gaMultiBitmap; // Each bit means that the page is being used multiple times
38 Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS; // Reference Counts
39 tPAddr giFirstFreePage; // First possibly free page
40 Uint64 giPhysRangeFree[NUM_MM_PHYS_RANGES]; // Number of free pages in each range
41 Uint64 giPhysRangeFirst[NUM_MM_PHYS_RANGES]; // First free page in each range
42 Uint64 giPhysRangeLast[NUM_MM_PHYS_RANGES]; // Last free page in each range
43 Uint64 giMaxPhysPage = 0; // Maximum Physical page
44 // Only used in init, allows the init code to provide pages for use by
45 // the allocator before the bitmaps exist.
46 // 3 entries because the are three calls to MM_AllocPhys in MM_Map
47 #define NUM_STATIC_ALLOC 3
48 tPAddr gaiStaticAllocPages[NUM_STATIC_ALLOC] = {0};
52 * \brief Initialise the physical memory map using a Multiboot 1 map
54 void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
56 tMBoot_MMapEnt *mmapStart;
59 int numPages, superPages;
63 tPAddr paddr, firstFreePage;
65 ENTER("pMBoot=%p", MBoot);
67 // Scan the physical memory map
68 // Looking for the top of physical memory
69 mmapStart = (void *)( KERNEL_BASE | MBoot->MMapAddr );
70 LOG("mmapStart = %p", mmapStart);
72 while( (Uint)ent < (Uint)mmapStart + MBoot->MMapLength )
74 // Adjust for the size of the entry
76 LOG("ent={Type:%i,Base:0x%x,Length:%x",
77 ent->Type, ent->Base, ent->Length);
79 // If entry is RAM and is above `maxAddr`, change `maxAddr`
80 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
81 maxAddr = ent->Base + ent->Length;
84 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
87 // Did we find a valid end?
89 // No, darn, let's just use the HighMem hack
90 giMaxPhysPage = (MBoot->HighMem >> 2) + 256; // HighMem is a kByte value
93 // Goodie, goodie gumdrops
94 giMaxPhysPage = maxAddr >> 12;
96 LOG("giMaxPhysPage = 0x%x", giMaxPhysPage);
98 // Find a contigous section of memory to hold it in
99 // - Starting from the end of the kernel
100 // - We also need a region for the super bitmap
101 superPages = ((giMaxPhysPage+64*8-1)/(64*8) + 0xFFF) >> 12;
102 numPages = (giMaxPhysPage + 7) / 8;
103 numPages = (numPages + 0xFFF) >> 12;
104 LOG("numPages = %i, superPages = %i", numPages, superPages);
107 int todo = numPages*2 + superPages;
108 // Ok, naieve allocation, just put it after the kernel
109 // - Allocated Bitmap
110 vaddr = MM_PAGE_BITMAP;
111 paddr = (tPAddr)&gKernelEnd - KERNEL_BASE;
115 for( i = 0; i < NUM_STATIC_ALLOC; i++) {
116 if(gaiStaticAllocPages[i] != 0) continue;
117 gaiStaticAllocPages[i] = paddr;
121 MM_Map(vaddr, paddr);
127 if( todo == numPages + superPages )
128 vaddr = MM_PAGE_DBLBMP;
129 if( todo == superPages )
130 vaddr = MM_PAGE_SUPBMP;
133 // Scan for a nice range
136 int todo = numPages*2 + superPages;
138 vaddr = MM_PAGE_BITMAP;
142 (Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
143 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size )
152 // Let's not put it below the kernel, shall we?
153 if( ent->Base + ent->Size < (tPAddr)&gKernelBase )
156 LOG("%x <= %x && %x > %x",
157 ent->Base, (tPAddr)&gKernelBase,
158 ent->Base + ent->Size, (tPAddr)&gKernelEnd - KERNEL_BASE
160 // Check if the kernel is in this range
161 if( ent->Base <= (tPAddr)&gKernelBase
162 && ent->Base + ent->Length > (tPAddr)&gKernelEnd - KERNEL_BASE )
164 avail = ent->Length >> 12;
165 avail -= ((tPAddr)&gKernelEnd - KERNEL_BASE - ent->Base) >> 12;
166 paddr = (tPAddr)&gKernelEnd - KERNEL_BASE;
168 // No? then we can use all of the block
171 avail = ent->Length >> 12;
175 Log(" MM_InitPhys_Multiboot: paddr=0x%x, avail=%i", paddr, avail);
178 while( todo && avail --)
180 // Static Allocations
181 for( i = 0; i < NUM_STATIC_ALLOC && avail; i++) {
182 if(gaiStaticAllocPages[i] != 0) continue;
183 gaiStaticAllocPages[i] = paddr;
190 MM_Map(vaddr, paddr);
195 // Alter the destination address when needed
196 if(todo == superPages+numPages)
197 vaddr = MM_PAGE_DBLBMP;
198 if(todo == superPages)
199 vaddr = MM_PAGE_SUPBMP;
202 // Fast quit if there's nothing left to allocate
206 // Save the current value of paddr to simplify the allocation later
207 firstFreePage = paddr;
209 LOG("Clearing multi bitmap");
211 memset(gaMultiBitmap, 0, numPages<<12);
212 // - initialise to one, then clear the avaliable areas
213 memset(gaMainBitmap, -1, numPages<<12);
214 LOG("Setting main bitmap");
215 // - Clear all Type=1 areas
216 LOG("Clearing valid regions");
219 (Uint)ent < (Uint)mmapStart + MBoot->MMapLength;
220 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size )
223 // Check if the type is RAM
224 if(ent->Type != 1) continue;
227 base = ent->Base >> 12;
228 size = ent->Size >> 12;
231 Uint64 val = -1L << (base & 63);
232 gaMainBitmap[base / 64] &= ~val;
234 base += 64 - (base & 63);
236 memset( &gaMainBitmap[base / 64], 0, size/8 );
238 Uint64 val = -1L << (size & 7);
240 gaMainBitmap[base / 64] &= ~val;
244 base = ent->Base >> 12;
245 size = ent->Size >> 12;
246 size = (size + (base & 63) + 63) >> 6;
249 Uint64 val = -1L << (base & 63);
250 gaSuperBitmap[base / 64] &= ~val;
252 base += 64 - (base & 63);
256 // Reference the used pages
257 base = (tPAddr)&gKernelBase >> 12;
258 size = firstFreePage >> 12;
259 memset( &gaMainBitmap[base / 64], -1, size/8 );
261 Uint64 val = -1L << (size & 7);
263 gaMainBitmap[base / 64] |= val;
266 // Free the unused static allocs
267 for( i = 0; i < NUM_STATIC_ALLOC; i++) {
268 if(gaiStaticAllocPages[i] != 0)
270 gaMainBitmap[ gaiStaticAllocPages[i] >> (12+6) ]
271 &= ~(1L << ((gaiStaticAllocPages[i]>>12)&63));
274 // Fill the super bitmap
275 LOG("Filling super bitmap");
276 memset(gaSuperBitmap, 0, superPages<<12);
277 for( base = 0; base < (size+63)/64; base ++)
279 if( gaMainBitmap[ base ] + 1 == 0 )
280 gaSuperBitmap[ base/64 ] |= 1L << (base&63);
283 // Set free page counts
284 for( base = 1; base < giMaxPhysPage; base ++ )
288 if( gaMainBitmap[ base >> 6 ] & (1L << (base&63)) ) continue;
291 rangeID = MM_int_GetRangeID( base << 12 );
293 // Increment free page count
294 giPhysRangeFree[ rangeID ] ++;
296 // Check for first free page in range
297 if(giPhysRangeFirst[ rangeID ] == 0)
298 giPhysRangeFirst[ rangeID ] = base;
299 // Set last (when the last free page is reached, this won't be
300 // updated anymore, hence will be correct)
301 giPhysRangeLast[ rangeID ] = base;
308 * \brief Allocate a contiguous range of physical pages with a maximum
309 * bit size of \a Bits
310 * \param Num Number of pages to allocate
311 * \param Bits Maximum size of the physical address
312 * \note If \a Bits is <= 0, any sized address is used (with preference
313 * to higher addresses)
315 tPAddr MM_AllocPhysRange(int Num, int Bits)
321 ENTER("iNum iBits", Num, Bits);
323 if( Bits <= 0 || Bits >= 64 ) // Speedup for the common case
324 rangeID = MM_PHYS_MAX;
326 rangeID = MM_int_GetRangeID( (1L << Bits) - 1 );
328 LOG("rangeID = %i", rangeID);
330 LOCK(&glPhysicalPages);
332 // Check if the range actually has any free pages
333 while(giPhysRangeFree[rangeID] == 0 && rangeID)
336 LOG("rangeID = %i", rangeID);
338 // What the? Oh, man. No free pages
339 if(giPhysRangeFree[rangeID] == 0) {
340 RELEASE(&glPhysicalPages);
343 Warning(" MM_AllocPhysRange: Out of free pages");
345 "Out of memory (unable to fulfil request for %i pages), zero remaining",
352 // Check if there is enough in the range
353 if(giPhysRangeFree[rangeID] >= Num)
355 LOG("{%i,0x%x -> 0x%x}",
356 giPhysRangeFree[rangeID],
357 giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
359 // Do a cheap scan, scanning upwards from the first free page in
362 addr = giPhysRangeFirst[ rangeID ];
363 while( addr <= giPhysRangeLast[ rangeID ] )
365 //Log(" MM_AllocPhysRange: addr = 0x%x", addr);
366 // Check the super bitmap
367 if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) {
368 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
371 addr &= ~0xFFF; // (1L << 6+6) - 1
374 // Check page block (64 pages)
375 if( gaMainBitmap[addr >> 6] + 1 == 0) {
376 LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr);
382 // Check individual page
383 if( gaMainBitmap[addr >> 6] & (1L << (addr & 63)) ) {
384 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
391 LOG("nFree(%i) == %i (0x%x)", nFree, Num, addr);
395 LOG("nFree = %i", nFree);
396 // If we don't find a contiguous block, nFree will not be equal
397 // to Num, so we set it to zero and do the expensive lookup.
398 if(nFree != Num) nFree = 0;
403 // Oops. ok, let's do an expensive check (scan down the list
404 // until a free range is found)
406 addr = giPhysRangeLast[ rangeID ];
408 RELEASE(&glPhysicalPages);
411 Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Num);
413 "Out of memory (unable to fulfil request for %i pages)",
419 LOG("nFree = %i, addr = 0x%08x", nFree, addr);
421 // Mark pages as allocated
423 for( i = 0; i < Num; i++, addr++ )
425 gaMainBitmap[addr >> 6] |= 1L << (addr & 63);
426 rangeID = MM_int_GetRangeID(addr << 12);
427 giPhysRangeFree[ rangeID ] --;
428 LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
429 if(addr == giPhysRangeFirst[ rangeID ])
430 giPhysRangeFirst[ rangeID ] += 1;
432 ret = addr; // Save the return address
434 // Update super bitmap
435 Num += addr & (64-1);
437 Num = (Num + (64-1)) & ~(64-1);
438 for( i = 0; i < Num/64; i++ )
440 if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
441 gaSuperBitmap[addr>>12] |= 1L << ((addr >> 6) & 63);
444 RELEASE(&glPhysicalPages);
445 LEAVE('x', ret << 12);
450 * \brief Allocate a single physical page, with no preference as to address
453 tPAddr MM_AllocPhys(void)
457 // Hack to allow allocation during setup
458 for(i = 0; i < NUM_STATIC_ALLOC; i++) {
459 if( gaiStaticAllocPages[i] ) {
460 tPAddr ret = gaiStaticAllocPages[i];
461 gaiStaticAllocPages[i] = 0;
462 Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
467 return MM_AllocPhysRange(1, -1);
471 * \brief Reference a physical page
473 void MM_RefPhys(tPAddr PAddr)
475 Uint64 page = PAddr >> 12;
477 if( PAddr >> 12 > giMaxPhysPage ) return ;
479 if( gaMainBitmap[ page >> 6 ] & (1L << (page&63)) )
482 gaMultiBitmap[ page >> 6 ] |= 1L << (page&63);
483 gaiPageReferences[ page ] ++;
488 gaMainBitmap[page >> 6] |= 1L << (page&63);
489 if( gaMainBitmap[page >> 6 ] + 1 == 0 )
490 gaSuperBitmap[page>> 12] |= 1L << ((page >> 6) & 63);
495 * \brief Dereference a physical page
497 void MM_DerefPhys(tPAddr PAddr)
499 Uint64 page = PAddr >> 12;
501 if( PAddr >> 12 > giMaxPhysPage ) return ;
503 if( gaMultiBitmap[ page >> 6 ] & (1L << (page&63)) ) {
504 gaiPageReferences[ page ] --;
505 if( gaiPageReferences[ page ] == 1 )
506 gaMultiBitmap[ page >> 6 ] &= ~(1L << (page&63));
507 if( gaiPageReferences[ page ] == 0 )
508 gaMainBitmap[ page >> 6 ] &= ~(1L << (page&63));
511 gaMainBitmap[ page >> 6 ] &= ~(1L << (page&63));
513 // Update the free counts if the page was freed
514 if( !(gaMainBitmap[ page >> 6 ] & (1L << (page&63))) )
517 rangeID = MM_int_GetRangeID( PAddr );
518 giPhysRangeFree[ rangeID ] ++;
519 if( giPhysRangeFirst[rangeID] > page )
520 giPhysRangeFirst[rangeID] = page;
521 if( giPhysRangeLast[rangeID] < page )
522 giPhysRangeLast[rangeID] = page;
525 // If the bitmap entry is not -1, unset the bit in the super bitmap
526 if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
527 gaSuperBitmap[page >> 12] &= ~(1L << ((page >> 6) & 63));
532 * \brief Takes a physical address and returns the ID of its range
533 * \param Addr Physical address of page
534 * \return Range ID from eMMPhys_Ranges
536 int MM_int_GetRangeID( tPAddr Addr )
541 return MM_PHYS_32BIT;
543 return MM_PHYS_24BIT;
545 return MM_PHYS_20BIT;
547 return MM_PHYS_16BIT;