Sorting source tree a bit
[tpg/acess2.git] / KernelLand / Kernel / include / tpl_mm_phys_stack.h
1 /*
2  * Acess2 Core
3  * 
4  * include/tpl_mm_phys.h
5  * Physical Memory Manager Template
6  */
7 #define DEBUG   0
8
9 /**
10  * \file tpl_mm_phys_stack.h
11  * \brief Template physical memory manager
12  *
13  * An extensible physical memory manager
14  *
15  * Usage: Requires NUM_MM_PHYS_RANGES to be set to the number of address
16  * "classes" wanted.
17  * MAX_PHYS_PAGES - Used to calculate structure sizes
18  * PADDR_TYPE
19  */
20
21 // === PROTOTYPES ===
22 //void  MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
23 //tPAddr        MM_AllocPhysRange(int Num, int Bits);
24 //tPAddr        MM_AllocPhys(void);
25 //void  MM_RefPhys(tPAddr PAddr);
26 //void  MM_DerefPhys(tPAddr PAddr);
27  int    MM_int_GetRangeID( tPAddr Addr );
28  int    MM_int_IsPhysUnavail( tPageNum Page );
29
30 // === GLOBALS ===
31 tMutex  glPhysicalPages;
32 //Uint64        *gaPageStacks[NUM_MM_PHYS_RANGES];      // Page stacks
33  int    giPageStackSizes[NUM_MM_PHYS_RANGES];   // Points to the first unused slot
34 Uint32  *gaiPageReferences = (void*)MM_PAGE_COUNTS;     // Reference Counts
35 Uint64  giMaxPhysPage = 0;      // Maximum Physical page
36
37 // === CODE ===
38 /**
39  * \brief Initialise the physical memory map using a Multiboot 1 map
40  */
41 void MM_Tpl_InitPhys(int MaxRAMPage)
42 {
43         const int       PAGE_SIZE = 0x1000;
44         const int       pagesPerPageOnStack = PAGE_SIZE / sizeof(gaPageStack[0]);
45          int    i;
46         tPageNum        page;
47
48 //      ENTER("pMBoot=%p", MBoot);
49         
50         giMaxPhysPage = MaxRAMPage;
51         
52         tPAddr  page = 0;
53         for( i = 0; i < NUM_MM_PHYS_RANGES; i ++ )
54         {
55                 for( ; page < giPageRangeMax[i] && page < giMaxPhysPage; page ++ )
56                 {
57                          int    rangeSize;
58
59                         rangeSize = MM_int_IsPhysUnavail(page);
60                         if( rangeSize > 0 ) {
61                                 page += rangeSize;
62                                 continue;
63                         }
64                         // Page is avaliable for use
65         
66                         // Check if the page stack is allocated
67                         tVAddr  stack_page = &gaPageStacks[i][giPageStackSizes[i]&~pagesPerPageOnStack];
68                         if( !MM_GetPhysAddr( stack_page ) ) {
69                                 MM_Map( stack_page, page*PAGE_SIZE );
70                         }
71                         else {
72                                 gaPageStacks[i][ giPageStackSizes[i] ] = page;
73                                 giPageStackSizes[i] ++;
74                         }
75                 }
76         }
77         
78         LEAVE('-');
79 }
80
81 /**
82  * \brief Allocate a contiguous range of physical pages with a maximum
83  *        bit size of \a MaxBits
84  * \param Pages Number of pages to allocate
85  * \param MaxBits       Maximum size of the physical address
86  * \note If \a MaxBits is <= 0, any sized address is used (with preference
87  *       to higher addresses)
88  */
89 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
90 {
91         tPAddr  addr, ret;
92          int    rangeID;
93          int    nFree = 0, i;
94         
95         ENTER("iPages iBits", Pages, MaxBits);
96         
97         if( MaxBits <= 0 || MaxBits >= 64 )     // Speedup for the common case
98                 rangeID = MM_PHYS_MAX;
99         else
100                 rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 );
101         
102         LOG("rangeID = %i", rangeID);
103         
104         Mutex_Acquire(&glPhysicalPages);
105         
106         // Check if the range actually has any free pages
107         while(giPageStackSizes[rangeID] == 0 && rangeID)
108                 rangeID --;
109         
110         LOG("rangeID = %i", rangeID);
111         
112         // What the? Oh, man. No free pages
113         if(giPageStackSizes[rangeID] == 0) {
114                 Mutex_Release(&glPhysicalPages);
115                 // TODO: Page out / attack the cache
116                 // ATM. Just Warning
117                 Warning(" MM_AllocPhysRange: Out of free pages");
118                 Log_Warning("Arch",
119                         "Out of memory (unable to fulfil request for %i pages), zero remaining",
120                         Pages
121                         );
122                 LEAVE('i', 0);
123                 return 0;
124         }
125         
126         // Check if there is enough in the range
127         if(giPhysRangeFree[rangeID] >= Pages)
128         {
129                 LOG("{%i,0x%x -> 0x%x}",
130                         giPhysRangeFree[rangeID],
131                         giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
132                         );
133                 // Do a cheap scan, scanning upwards from the first free page in
134                 // the range
135                 nFree = 0;
136                 addr = giPhysRangeFirst[ rangeID ];
137                 while( addr <= giPhysRangeLast[ rangeID ] )
138                 {
139                         //Log(" MM_AllocPhysRange: addr = 0x%x", addr);
140                         // Check the super bitmap
141                         if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) {
142                                 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
143                                 nFree = 0;
144                                 addr += 1LL << (6+6);
145                                 addr &= ~0xFFF; // (1LL << 6+6) - 1
146                                 continue;
147                         }
148                         // Check page block (64 pages)
149                         if( gaMainBitmap[addr >> 6] + 1 == 0) {
150                                 LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr);
151                                 nFree = 0;
152                                 addr += 1LL << (6);
153                                 addr &= ~0x3F;
154                                 continue;
155                         }
156                         // Check individual page
157                         if( gaMainBitmap[addr >> 6] & (1LL << (addr & 63)) ) {
158                                 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
159                                 nFree = 0;
160                                 addr ++;
161                                 continue;
162                         }
163                         nFree ++;
164                         addr ++;
165                         LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr);
166                         if(nFree == Pages)
167                                 break;
168                 }
169                 LOG("nFree = %i", nFree);
170                 // If we don't find a contiguous block, nFree will not be equal
171                 // to Num, so we set it to zero and do the expensive lookup.
172                 if(nFree != Pages)      nFree = 0;
173         }
174         
175         if( !nFree )
176         {
177                 // Oops. ok, let's do an expensive check (scan down the list
178                 // until a free range is found)
179                 nFree = 1;
180                 addr = giPhysRangeLast[ rangeID ];
181                 // TODO
182                 Mutex_Release(&glPhysicalPages);
183                 // TODO: Page out
184                 // ATM. Just Warning
185                 Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Pages);
186                 Log_Warning("Arch",
187                         "Out of memory (unable to fulfil request for %i pages)",
188                         Pages   
189                         );
190                 LEAVE('i', 0);
191                 return 0;
192         }
193         LOG("nFree = %i, addr = 0x%08x", nFree, addr);
194         
195         // Mark pages as allocated
196         addr -= Pages;
197         for( i = 0; i < Pages; i++, addr++ )
198         {
199                 gaMainBitmap[addr >> 6] |= 1LL << (addr & 63);
200                 rangeID = MM_int_GetRangeID(addr << 12);
201                 giPhysRangeFree[ rangeID ] --;
202                 LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
203                 if(addr == giPhysRangeFirst[ rangeID ])
204                         giPhysRangeFirst[ rangeID ] += 1;
205         }
206         ret = addr;     // Save the return address
207         
208         // Update super bitmap
209         Pages += addr & (64-1);
210         addr &= ~(64-1);
211         Pages = (Pages + (64-1)) & ~(64-1);
212         for( i = 0; i < Pages/64; i++ )
213         {
214                 if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
215                         gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63);
216         }
217         
218         Mutex_Release(&glPhysicalPages);
219         LEAVE('x', ret << 12);
220         return ret << 12;
221 }
222
223 /**
224  * \brief Allocate a single physical page, with no preference as to address
225  *        size.
226  */
227 tPAddr MM_AllocPhys(void)
228 {
229          int    i;
230         
231         // Hack to allow allocation during setup
232         for(i = 0; i < NUM_STATIC_ALLOC; i++) {
233                 if( gaiStaticAllocPages[i] ) {
234                         tPAddr  ret = gaiStaticAllocPages[i];
235                         gaiStaticAllocPages[i] = 0;
236                         Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
237                         return ret;
238                 }
239         }
240         
241         return MM_AllocPhysRange(1, -1);
242 }
243
244 /**
245  * \brief Reference a physical page
246  */
247 void MM_RefPhys(tPAddr PAddr)
248 {
249         Uint64  page = PAddr >> 12;
250         
251         if( PAddr >> 12 > giMaxPhysPage )       return ;
252         
253         if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) )
254         {
255                 // Reference again
256                 gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63);
257                 gaiPageReferences[ page ] ++;
258         }
259         else
260         {
261                 // Allocate
262                 gaMainBitmap[page >> 6] |= 1LL << (page&63);
263                 if( gaMainBitmap[page >> 6 ] + 1 == 0 )
264                         gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63);
265         }
266 }
267
268 /**
269  * \brief Dereference a physical page
270  */
271 void MM_DerefPhys(tPAddr PAddr)
272 {
273         Uint64  page = PAddr >> 12;
274         
275         if( PAddr >> 12 > giMaxPhysPage )       return ;
276         
277         if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) {
278                 gaiPageReferences[ page ] --;
279                 if( gaiPageReferences[ page ] == 1 )
280                         gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63));
281                 if( gaiPageReferences[ page ] == 0 )
282                         gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
283         }
284         else
285                 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
286         
287         // Update the free counts if the page was freed
288         if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) )
289         {
290                  int    rangeID;
291                 rangeID = MM_int_GetRangeID( PAddr );
292                 giPhysRangeFree[ rangeID ] ++;
293                 if( giPhysRangeFirst[rangeID] > page )
294                         giPhysRangeFirst[rangeID] = page;
295                 if( giPhysRangeLast[rangeID] < page )
296                         giPhysRangeLast[rangeID] = page;
297         }
298         
299         // If the bitmap entry is not -1, unset the bit in the super bitmap
300         if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
301                 gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
302         }
303 }
304
305 /**
306  * \brief Takes a physical address and returns the ID of its range
307  * \param Addr  Physical address of page
308  * \return Range ID from eMMPhys_Ranges
309  */
310 int MM_int_GetRangeID( tPAddr Addr )
311 {
312         if(Addr >> 32)
313                 return MM_PHYS_MAX;
314         else if(Addr >> 24)
315                 return MM_PHYS_32BIT;
316         else if(Addr >> 20)
317                 return MM_PHYS_24BIT;
318         else if(Addr >> 16)
319                 return MM_PHYS_20BIT;
320         else
321                 return MM_PHYS_16BIT;
322 }

UCC git Repository :: git.ucc.asn.au