Usermode/libc - Fix strchr and strrchr behavior
[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 void MM_DumpStatistics(void)
82 {
83         // TODO: PM Statistics for tpl_mm_phys_bitmap
84 }
85
86 /**
87  * \brief Allocate a contiguous range of physical pages with a maximum
88  *        bit size of \a MaxBits
89  * \param Pages Number of pages to allocate
90  * \param MaxBits       Maximum size of the physical address
91  * \note If \a MaxBits is <= 0, any sized address is used (with preference
92  *       to higher addresses)
93  */
94 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
95 {
96         tPAddr  addr, ret;
97          int    rangeID;
98          int    nFree = 0, i;
99         
100         ENTER("iPages iBits", Pages, MaxBits);
101         
102         if( MaxBits <= 0 || MaxBits >= 64 )     // Speedup for the common case
103                 rangeID = MM_PHYS_MAX;
104         else
105                 rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 );
106         
107         LOG("rangeID = %i", rangeID);
108         
109         Mutex_Acquire(&glPhysicalPages);
110         
111         // Check if the range actually has any free pages
112         while(giPageStackSizes[rangeID] == 0 && rangeID)
113                 rangeID --;
114         
115         LOG("rangeID = %i", rangeID);
116         
117         // What the? Oh, man. No free pages
118         if(giPageStackSizes[rangeID] == 0) {
119                 Mutex_Release(&glPhysicalPages);
120                 // TODO: Page out / attack the cache
121                 // ATM. Just Warning
122                 Warning(" MM_AllocPhysRange: Out of free pages");
123                 Log_Warning("Arch",
124                         "Out of memory (unable to fulfil request for %i pages), zero remaining",
125                         Pages
126                         );
127                 LEAVE('i', 0);
128                 return 0;
129         }
130         
131         // Check if there is enough in the range
132         if(giPhysRangeFree[rangeID] >= Pages)
133         {
134                 LOG("{%i,0x%x -> 0x%x}",
135                         giPhysRangeFree[rangeID],
136                         giPhysRangeFirst[rangeID], giPhysRangeLast[rangeID]
137                         );
138                 // Do a cheap scan, scanning upwards from the first free page in
139                 // the range
140                 nFree = 0;
141                 addr = giPhysRangeFirst[ rangeID ];
142                 while( addr <= giPhysRangeLast[ rangeID ] )
143                 {
144                         //Log(" MM_AllocPhysRange: addr = 0x%x", addr);
145                         // Check the super bitmap
146                         if( gaSuperBitmap[addr >> (6+6)] + 1 == 0 ) {
147                                 LOG("nFree = %i = 0 (super) (0x%x)", nFree, addr);
148                                 nFree = 0;
149                                 addr += 1LL << (6+6);
150                                 addr &= ~0xFFF; // (1LL << 6+6) - 1
151                                 continue;
152                         }
153                         // Check page block (64 pages)
154                         if( gaMainBitmap[addr >> 6] + 1 == 0) {
155                                 LOG("nFree = %i = 0 (main) (0x%x)", nFree, addr);
156                                 nFree = 0;
157                                 addr += 1LL << (6);
158                                 addr &= ~0x3F;
159                                 continue;
160                         }
161                         // Check individual page
162                         if( gaMainBitmap[addr >> 6] & (1LL << (addr & 63)) ) {
163                                 LOG("nFree = %i = 0 (page) (0x%x)", nFree, addr);
164                                 nFree = 0;
165                                 addr ++;
166                                 continue;
167                         }
168                         nFree ++;
169                         addr ++;
170                         LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr);
171                         if(nFree == Pages)
172                                 break;
173                 }
174                 LOG("nFree = %i", nFree);
175                 // If we don't find a contiguous block, nFree will not be equal
176                 // to Num, so we set it to zero and do the expensive lookup.
177                 if(nFree != Pages)      nFree = 0;
178         }
179         
180         if( !nFree )
181         {
182                 // Oops. ok, let's do an expensive check (scan down the list
183                 // until a free range is found)
184                 nFree = 1;
185                 addr = giPhysRangeLast[ rangeID ];
186                 // TODO
187                 Mutex_Release(&glPhysicalPages);
188                 // TODO: Page out
189                 // ATM. Just Warning
190                 Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Pages);
191                 Log_Warning("Arch",
192                         "Out of memory (unable to fulfil request for %i pages)",
193                         Pages   
194                         );
195                 LEAVE('i', 0);
196                 return 0;
197         }
198         LOG("nFree = %i, addr = 0x%08x", nFree, addr);
199         
200         // Mark pages as allocated
201         addr -= Pages;
202         for( i = 0; i < Pages; i++, addr++ )
203         {
204                 gaMainBitmap[addr >> 6] |= 1LL << (addr & 63);
205                 rangeID = MM_int_GetRangeID(addr << 12);
206                 giPhysRangeFree[ rangeID ] --;
207                 LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
208                 if(addr == giPhysRangeFirst[ rangeID ])
209                         giPhysRangeFirst[ rangeID ] += 1;
210         }
211         ret = addr;     // Save the return address
212         
213         // Update super bitmap
214         Pages += addr & (64-1);
215         addr &= ~(64-1);
216         Pages = (Pages + (64-1)) & ~(64-1);
217         for( i = 0; i < Pages/64; i++ )
218         {
219                 if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
220                         gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63);
221         }
222         
223         Mutex_Release(&glPhysicalPages);
224         LEAVE('x', ret << 12);
225         return ret << 12;
226 }
227
228 /**
229  * \brief Allocate a single physical page, with no preference as to address
230  *        size.
231  */
232 tPAddr MM_AllocPhys(void)
233 {
234          int    i;
235         
236         // Hack to allow allocation during setup
237         for(i = 0; i < NUM_STATIC_ALLOC; i++) {
238                 if( gaiStaticAllocPages[i] ) {
239                         tPAddr  ret = gaiStaticAllocPages[i];
240                         gaiStaticAllocPages[i] = 0;
241                         Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
242                         return ret;
243                 }
244         }
245         
246         return MM_AllocPhysRange(1, -1);
247 }
248
249 /**
250  * \brief Reference a physical page
251  */
252 void MM_RefPhys(tPAddr PAddr)
253 {
254         Uint64  page = PAddr >> 12;
255         
256         if( PAddr >> 12 > giMaxPhysPage )       return ;
257         
258         if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) )
259         {
260                 // Reference again
261                 gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63);
262                 gaiPageReferences[ page ] ++;
263         }
264         else
265         {
266                 // Allocate
267                 gaMainBitmap[page >> 6] |= 1LL << (page&63);
268                 if( gaMainBitmap[page >> 6 ] + 1 == 0 )
269                         gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63);
270         }
271 }
272
273 /**
274  * \brief Dereference a physical page
275  */
276 void MM_DerefPhys(tPAddr PAddr)
277 {
278         Uint64  page = PAddr >> 12;
279         
280         if( PAddr >> 12 > giMaxPhysPage )       return ;
281         
282         if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) {
283                 gaiPageReferences[ page ] --;
284                 if( gaiPageReferences[ page ] == 1 )
285                         gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63));
286                 if( gaiPageReferences[ page ] == 0 )
287                         gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
288         }
289         else
290                 gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
291         
292         // Update the free counts if the page was freed
293         if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) )
294         {
295                  int    rangeID;
296                 rangeID = MM_int_GetRangeID( PAddr );
297                 giPhysRangeFree[ rangeID ] ++;
298                 if( giPhysRangeFirst[rangeID] > page )
299                         giPhysRangeFirst[rangeID] = page;
300                 if( giPhysRangeLast[rangeID] < page )
301                         giPhysRangeLast[rangeID] = page;
302         }
303         
304         // If the bitmap entry is not -1, unset the bit in the super bitmap
305         if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
306                 gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
307         }
308 }
309
310 /**
311  * \brief Takes a physical address and returns the ID of its range
312  * \param Addr  Physical address of page
313  * \return Range ID from eMMPhys_Ranges
314  */
315 int MM_int_GetRangeID( tPAddr Addr )
316 {
317         if(Addr >> 32)
318                 return MM_PHYS_MAX;
319         else if(Addr >> 24)
320                 return MM_PHYS_32BIT;
321         else if(Addr >> 20)
322                 return MM_PHYS_24BIT;
323         else if(Addr >> 16)
324                 return MM_PHYS_20BIT;
325         else
326                 return MM_PHYS_16BIT;
327 }

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