Fixed a bug that caused physical memory allocation to be one-time use
[tpg/acess2.git] / Kernel / arch / x86 / mm_phys.c
1 /*
2  * Acess2
3  * - Physical memory manager
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <mboot.h>
8 #include <mm_virt.h>
9
10 #define USE_STACK       1
11
12 #define REFERENCE_BASE  0xE0400000
13
14 // === IMPORTS ===
15 extern void     gKernelEnd;
16
17 // === PROTOTYPES ===
18 tPAddr  MM_AllocPhys();
19 tPAddr  MM_AllocPhysRange(int Pages, int MaxBits);
20 void    MM_RefPhys(tPAddr Addr);
21 void    MM_DerefPhys(tPAddr Addr);
22
23 // === GLOBALS ===
24 Uint64  giPhysAlloc = 0;        // Number of allocated pages
25 Uint64  giPageCount = 0;        // Total number of pages
26 Uint64  giLastPossibleFree = 0; // Last possible free page (before all pages are used)
27
28 Uint32  gaSuperBitmap[1024];    // Blocks of 1024 Pages
29 Uint32  gaPageBitmap[1024*1024/32];     // Individual pages
30 Uint32  *gaPageReferences;
31
32 // === CODE ===
33 void MM_Install(tMBoot_Info *MBoot)
34 {
35         Uint    kernelPages, num;
36         Uint    i;
37         Uint64  maxAddr = 0;
38         tMBoot_Module   *mods;
39         tMBoot_MMapEnt  *ent;
40         
41         // --- Find largest address
42         MBoot->MMapAddr |= KERNEL_BASE;
43         ent = (void *)( MBoot->MMapAddr );
44         while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
45         {
46                 // Adjust for size
47                 ent->Size += 4;
48                 
49                 // If entry is RAM and is above `maxAddr`, change `maxAddr`
50                 if(ent->Type == 1 && ent->Base + ent->Length > maxAddr)
51                         maxAddr = ent->Base + ent->Length;
52                 // Go to next entry
53                 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
54         }
55         
56         if(maxAddr == 0) {      
57                 giPageCount = (MBoot->HighMem >> 2) + 256;      // HighMem is a kByte value
58         }
59         else {
60                 giPageCount = maxAddr >> 12;
61         }
62         giLastPossibleFree = giPageCount - 1;
63         
64         memsetd(gaPageBitmap, 0xFFFFFFFF, giPageCount/32);
65         
66         // Set up allocateable space
67         ent = (void *)( MBoot->MMapAddr );
68         while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
69         {               
70                 memsetd( &gaPageBitmap[ent->Base/(4096*32)], 0, ent->Length/(4096*32) );
71                 ent = (tMBoot_MMapEnt *)( (Uint)ent + ent->Size );
72         }
73         
74         // Get used page count
75         kernelPages = (Uint)&gKernelEnd - KERNEL_BASE - 0x100000;
76         kernelPages += 0xFFF;   // Page Align
77         kernelPages >>= 12;
78         
79         // Fill page bitmap
80         num = kernelPages/32;
81         memsetd( &gaPageBitmap[0x100000/(4096*32)], -1, num );
82         gaPageBitmap[ 0x100000/(4096*32) + num ] = (1 << (kernelPages & 31)) - 1;
83         
84         // Fill Superpage bitmap
85         num = kernelPages/(32*32);
86         memsetd( &gaSuperBitmap[0x100000/(4096*32*32)], -1, num );
87         gaSuperBitmap[ 0x100000/(4096*32*32) + num ] = (1 << ((kernelPages / 32) & 31)) - 1;
88         
89         // Mark Multiboot's pages as taken
90         // - Structure
91         MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
92         // - Module List
93         for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
94                 MM_RefPhys( MBoot->Modules + (i << 12) );
95         // - Modules
96         mods = (void*)(MBoot->Modules + KERNEL_BASE);
97         for(i = 0; i < MBoot->ModuleCount; i++)
98         {
99                 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
100                 while(num--)
101                         MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
102         }
103         
104         // Allocate References
105         //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
106         for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
107         {
108                 MM_Allocate( REFERENCE_BASE + (num<<12) );
109         }
110         
111         //LOG("Filling");
112         // Fill references
113         gaPageReferences = (void*)REFERENCE_BASE;
114         memsetd(gaPageReferences, 1, kernelPages);
115         for( num = kernelPages; num < giPageCount; num++ )
116         {
117                 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
118         }
119 }
120
121 /**
122  * \fn tPAddr MM_AllocPhys()
123  * \brief Allocates a physical page from the general pool
124  */
125 tPAddr MM_AllocPhys()
126 {
127         // int  a, b, c;
128          int    indx;
129         tPAddr  ret;
130         
131         ENTER("");
132         
133         LOCK( &giPhysAlloc );
134         
135         // Find free page
136         // Scan downwards
137         #if 1
138         LOG("giLastPossibleFree = %i", giLastPossibleFree);
139         for( indx = giLastPossibleFree; indx >= 0; )
140         {
141                 if( gaSuperBitmap[indx>>10] == -1 ) {
142                         indx -= 1024;
143                         continue;
144                 }
145                 if( gaPageBitmap[indx>>5] == -1 ) {
146                         indx -= 32;
147                         continue;
148                 }
149                 
150                 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
151                         indx --;
152                         continue;
153                 }
154                 break;
155         }
156         LOG("indx = %i", indx);
157         #else
158         c = giLastPossibleFree % 32;
159         b = (giLastPossibleFree / 32) % 32;
160         a = giLastPossibleFree / 1024;
161         
162         LOG("a=%i,b=%i,c=%i", a, b, c);
163         for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
164         if(a < 0) {
165                 RELEASE( &giPhysAlloc );
166                 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
167                 LEAVE('i', 0);
168                 return 0;
169         }
170         for( ; gaSuperBitmap[a] & (1<<b); b-- );
171         for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
172         LOG("a=%i,b=%i,c=%i", a, b, c);
173         indx = (a << 10) | (b << 5) | c;
174         #endif
175         
176         // Mark page used
177         if(gaPageReferences)
178                 gaPageReferences[ indx ] = 1;
179         gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
180         
181         
182         // Get address
183         ret = indx << 12;
184         giLastPossibleFree = indx;
185         
186         // Mark used block
187         if(gaPageBitmap[ indx>>5 ] == -1)
188                 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
189
190         // Release Spinlock
191         RELEASE( &giPhysAlloc );
192         
193         LEAVE('X', ret);
194         //Log("MM_AllocPhys: RETURN 0x%x", ret);
195         return ret;
196 }
197
198 /**
199  * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
200  * \brief Allocate a range of physical pages
201  * \param Pages Number of pages to allocate
202  * \param MaxBits       Maximum number of address bits to use
203  */
204 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
205 {
206          int    a, b;
207          int    i, idx, sidx;
208         tPAddr  ret;
209         
210         // Sanity Checks
211         if(MaxBits < 0) return 0;
212         if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
213         
214         // Lock
215         LOCK( &giPhysAlloc );
216         
217         // Set up search state
218         if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
219                 sidx = (tPAddr)1 << (MaxBits-12);
220         }
221         else {
222                 sidx = giLastPossibleFree;
223         }
224         idx = sidx / 32;
225         sidx %= 32;
226         b = idx % 32;
227         a = idx / 32;
228         
229         // Find free page
230         for( ; gaSuperBitmap[a] == -1 && a --; );
231         if(a < 0) {
232                 RELEASE( &giPhysAlloc );
233                 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
234                 return 0;
235         }
236         for( ; gaSuperBitmap[a] & (1 << b); b-- );
237         idx = a * 32 + b;
238         for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- );
239         
240         // Check if the gap is large enough
241         while( idx >= 0 )
242         {
243                 // Find a free page
244                 for( ; ; )
245                 {
246                         // Bulk Skip
247                         if( gaPageBitmap[idx] == -1 ) {
248                                 idx --;
249                                 sidx = 31;
250                                 continue;
251                         }
252                         
253                         if( gaPageBitmap[idx] & (1 << sidx) ) {
254                                 sidx --;
255                                 if(sidx < 0) {  sidx = 31;      idx --; }
256                                 if(idx < 0)     break;
257                                 continue;
258                         }
259                         break;
260                 }
261                 if( idx < 0 )   break;
262                 
263                 // Check if it is a free range
264                 for( i = 0; i < Pages; i++ )
265                 {
266                         // Used page? break
267                         if( gaPageBitmap[idx] & (1 << sidx) )
268                                 break;
269                         
270                         sidx --;
271                         if(sidx < 0) {  sidx = 31;      idx --; }
272                         if(idx < 0)     break;
273                 }
274                 
275                 if( i == Pages )
276                         break;
277         }
278         
279         // Check if an address was found
280         if( idx < 0 ) {
281                 RELEASE( &giPhysAlloc );
282                 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
283         }
284         
285         // Mark pages used
286         for( i = 0; i < Pages; i++ )
287         {
288                 if(gaPageReferences)
289                         gaPageReferences[idx*32+sidx] = 1;
290                 gaPageBitmap[ idx ] |= 1 << sidx;
291                 sidx ++;
292                 if(sidx == 32) {        sidx = 0;       idx ++; }
293         }
294         
295         // Get address
296         ret = (idx << 17) | (sidx << 12);
297         
298         // Mark used block
299         if(gaPageBitmap[ idx ] == -1)   gaSuperBitmap[idx/32] |= 1 << (idx%32);
300
301         // Release Spinlock
302         RELEASE( &giPhysAlloc );
303         
304         return ret;
305 }
306
307 /**
308  * \fn void MM_RefPhys(tPAddr Addr)
309  */
310 void MM_RefPhys(tPAddr Addr)
311 {
312         // Get page number
313         Addr >>= 12;
314         
315         // We don't care about non-ram pages
316         if(Addr >= giPageCount) return;
317         
318         // Lock Structures
319         LOCK( &giPhysAlloc );
320         
321         // Reference the page
322         if(gaPageReferences)
323                 gaPageReferences[ Addr ] ++;
324         
325         // Mark as used
326         gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
327         
328         // Mark used block
329         if(gaPageBitmap[ Addr / 32 ] == -1)     gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
330         
331         // Release Spinlock
332         RELEASE( &giPhysAlloc );
333 }
334
335 /**
336  * \fn void MM_DerefPhys(Uint32 Addr)
337  */
338 void MM_DerefPhys(tPAddr Addr)
339 {
340         // Get page number
341         Addr >>= 12;
342         
343         // We don't care about non-ram pages
344         if(Addr >= giPageCount) return;
345         
346         // Check if it is freed
347         if(gaPageReferences[ Addr ] == 0) {
348                 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
349                 return;
350         }
351         
352         // Lock Structures
353         LOCK( &giPhysAlloc );
354         
355         if( giLastPossibleFree < Addr )
356                 giLastPossibleFree = Addr;
357
358         // Dereference
359         gaPageReferences[ Addr ] --;
360         
361         // Mark as free in bitmaps
362         if( gaPageReferences[ Addr ] == 0 )
363         {
364                 //LOG("Freed 0x%x by %p\n", Addr<<12, __builtin_return_address(0));
365                 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
366                 if(gaPageReferences[ Addr ] == 0)
367                         gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
368         }
369         
370         // Release spinlock
371         RELEASE( &giPhysAlloc );
372 }
373
374 /**
375  * \fn int MM_GetRefCount(tPAddr Addr)
376  */
377 int MM_GetRefCount(tPAddr Addr)
378 {
379         // Get page number
380         Addr >>= 12;
381         
382         // We don't care about non-ram pages
383         if(Addr >= giPageCount) return -1;
384         
385         // Check if it is freed
386         return gaPageReferences[ Addr ];
387 }

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