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

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