3abe4133545c8ecf970a465ea00778fc05d8d198
[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 #define TRACE_ALLOCS    0       // Print trace messages on AllocPhys/DerefPhys
12
13 #define REFERENCE_BASE  0xE0400000
14
15 // === IMPORTS ===
16 extern void     gKernelEnd;
17
18 // === PROTOTYPES ===
19 tPAddr  MM_AllocPhys(void);
20 tPAddr  MM_AllocPhysRange(int Pages, int MaxBits);
21 void    MM_RefPhys(tPAddr PAddr);
22 void    MM_DerefPhys(tPAddr PAddr);
23
24 // === GLOBALS ===
25 tMutex  glPhysAlloc;
26 Uint64  giPhysAlloc = 0;        // Number of allocated pages
27 Uint64  giPageCount = 0;        // Total number of pages
28 Uint64  giLastPossibleFree = 0; // Last possible free page (before all pages are used)
29
30 Uint32  gaSuperBitmap[1024];    // Blocks of 1024 Pages
31 Uint32  gaPageBitmap[1024*1024/32];     // Individual pages
32 Uint32  *gaPageReferences;
33
34 // === CODE ===
35 void MM_Install(tMBoot_Info *MBoot)
36 {
37         Uint    kernelPages, num;
38         Uint    i;
39         Uint64  maxAddr = 0;
40         tMBoot_Module   *mods;
41         tMBoot_MMapEnt  *ent;
42         
43         // --- Find largest address
44         MBoot->MMapAddr |= KERNEL_BASE;
45         ent = (void *)( MBoot->MMapAddr );
46         while( (Uint)ent < MBoot->MMapAddr + MBoot->MMapLength )
47         {
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         // - Module List
95         for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
96                 MM_RefPhys( MBoot->Modules + (i << 12) );
97         // - Modules
98         mods = (void*)(MBoot->Modules + KERNEL_BASE);
99         for(i = 0; i < MBoot->ModuleCount; i++)
100         {
101                 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
102                 while(num--)
103                         MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
104         }
105         
106         // Allocate References
107         //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
108         for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
109         {
110                 MM_Allocate( REFERENCE_BASE + (num<<12) );
111         }
112         
113         //LOG("Filling");
114         // Fill references
115         gaPageReferences = (void*)REFERENCE_BASE;
116         memsetd(gaPageReferences, 1, kernelPages);
117         for( num = kernelPages; num < giPageCount; num++ )
118         {
119                 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
120         }
121 }
122
123 /**
124  * \fn tPAddr MM_AllocPhys(void)
125  * \brief Allocates a physical page from the general pool
126  */
127 tPAddr MM_AllocPhys(void)
128 {
129         // int  a, b, c;
130          int    indx;
131         tPAddr  ret;
132         
133         ENTER("");
134         
135         Mutex_Acquire( &glPhysAlloc );
136         
137         // Find free page
138         // Scan downwards
139         #if 1
140         LOG("giLastPossibleFree = %i", giLastPossibleFree);
141         for( indx = giLastPossibleFree; indx >= 0; )
142         {
143                 if( gaSuperBitmap[indx>>10] == -1 ) {
144                         indx -= 1024;
145                         continue;
146                 }
147                 
148                 if( gaPageBitmap[indx>>5] == -1 ) {
149                         indx -= 32;
150                         continue;
151                 }
152                 
153                 if( gaPageBitmap[indx>>5] & (1 << (indx&31)) ) {
154                         indx --;
155                         continue;
156                 }
157                 break;
158         }
159         LOG("indx = %i", indx);
160         #else
161         c = giLastPossibleFree % 32;
162         b = (giLastPossibleFree / 32) % 32;
163         a = giLastPossibleFree / 1024;
164         
165         LOG("a=%i,b=%i,c=%i", a, b, c);
166         for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
167         if(a < 0) {
168                 Mutex_Release( &glPhysAlloc );
169                 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
170                 LEAVE('i', 0);
171                 return 0;
172         }
173         for( ; gaSuperBitmap[a] & (1<<b); b-- );
174         for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
175         LOG("a=%i,b=%i,c=%i", a, b, c);
176         indx = (a << 10) | (b << 5) | c;
177         #endif
178         
179         if( indx < 0 ) {
180                 Mutex_Release( &glPhysAlloc );
181                 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
182                 LEAVE('i', 0);
183                 return 0;
184         }
185         
186         if( indx > 0xFFFFF ) {
187                 Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
188         }
189         
190         // Mark page used
191         if(gaPageReferences)
192                 gaPageReferences[ indx ] = 1;
193         gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
194         
195         giPhysAlloc ++;
196         
197         // Get address
198         ret = indx << 12;
199         giLastPossibleFree = indx;
200         
201         // Mark used block
202         if(gaPageBitmap[ indx>>5 ] == -1)
203                 gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
204
205         // Release Spinlock
206         Mutex_Release( &glPhysAlloc );
207         
208         LEAVE('X', ret);
209         #if TRACE_ALLOCS
210         Log_Debug("PMem", "MM_AllocPhys: RETURN 0x%llx (%i free)", ret, giPageCount-giPhysAlloc);
211         #endif
212         return ret;
213 }
214
215 /**
216  * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
217  * \brief Allocate a range of physical pages
218  * \param Pages Number of pages to allocate
219  * \param MaxBits       Maximum number of address bits to use
220  */
221 tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
222 {
223          int    a, b;
224          int    i, idx, sidx;
225         tPAddr  ret;
226         
227         ENTER("iPages iMaxBits", Pages, MaxBits);
228         
229         // Sanity Checks
230         if(MaxBits < 0) {
231                 LEAVE('i', 0);
232                 return 0;
233         }
234         if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
235         
236         // Lock
237         Mutex_Acquire( &glPhysAlloc );
238         
239         // Set up search state
240         if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
241                 sidx = (tPAddr)1 << (MaxBits-12);
242         }
243         else {
244                 sidx = giLastPossibleFree;
245         }
246         idx = sidx / 32;
247         sidx %= 32;
248         b = idx % 32;
249         a = idx / 32;
250         
251         #if 0
252         LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
253         
254         // Find free page
255         for( ; gaSuperBitmap[a] == -1 && a --; )        b = 31;
256         if(a < 0) {
257                 Mutex_Release( &glPhysAlloc );
258                 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
259                 LEAVE('i', 0);
260                 return 0;
261         }
262         LOG("a = %i", a);
263         for( ; gaSuperBitmap[a] & (1 << b); b-- )       sidx = 31;
264         LOG("b = %i", b);
265         idx = a * 32 + b;
266         for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
267                 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
268         
269         LOG("idx = %i, sidx = %i", idx, sidx);
270         #else
271         
272         #endif
273         
274         // Check if the gap is large enough
275         while( idx >= 0 )
276         {
277                 // Find a free page
278                 for( ; ; )
279                 {
280                         // Bulk Skip
281                         if( gaPageBitmap[idx] == -1 ) {
282                                 idx --;
283                                 sidx = 31;
284                                 continue;
285                         }
286                         
287                         if( gaPageBitmap[idx] & (1 << sidx) ) {
288                                 sidx --;
289                                 if(sidx < 0) {  sidx = 31;      idx --; }
290                                 if(idx < 0)     break;
291                                 continue;
292                         }
293                         break;
294                 }
295                 if( idx < 0 )   break;
296                 
297                 // Check if it is a free range
298                 for( i = 0; i < Pages; i++ )
299                 {
300                         // Used page? break
301                         if( gaPageBitmap[idx] & (1 << sidx) )
302                                 break;
303                         
304                         sidx --;
305                         if(sidx < 0) {  sidx = 31;      idx --; }
306                         if(idx < 0)     break;
307                 }
308                 
309                 if( i == Pages )
310                         break;
311         }
312         
313         // Check if an address was found
314         if( idx < 0 ) {
315                 Mutex_Release( &glPhysAlloc );
316                 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
317                 LEAVE('i', 0);
318                 return 0;
319         }
320         
321         // Mark pages used
322         for( i = 0; i < Pages; i++ )
323         {
324                 if(gaPageReferences)
325                         gaPageReferences[idx*32+sidx] = 1;
326                 gaPageBitmap[ idx ] |= 1 << sidx;
327                 sidx ++;
328                 giPhysAlloc ++;
329                 if(sidx == 32) { sidx = 0;      idx ++; }
330         }
331         
332         // Get address
333         ret = (idx << 17) | (sidx << 12);
334         
335         // Mark used block
336         if(gaPageBitmap[ idx ] == -1)   gaSuperBitmap[idx/32] |= 1 << (idx%32);
337
338         // Release Spinlock
339         Mutex_Release( &glPhysAlloc );
340         
341         LEAVE('X', ret);
342         #if TRACE_ALLOCS
343         Log_Debug("PMem", "MM_AllocPhysRange: RETURN 0x%llx-0x%llx (%i free)",
344                 ret, ret + (1<<Pages)-1, giPageCount-giPhysAlloc);
345         #endif
346         return ret;
347 }
348
349 /**
350  * \fn void MM_RefPhys(tPAddr PAddr)
351  */
352 void MM_RefPhys(tPAddr PAddr)
353 {
354         // Get page number
355         PAddr >>= 12;
356         
357         // We don't care about non-ram pages
358         if(PAddr >= giPageCount)        return;
359         
360         // Lock Structures
361         Mutex_Acquire( &glPhysAlloc );
362         
363         // Reference the page
364         if(gaPageReferences)
365                 gaPageReferences[ PAddr ] ++;
366         
367         // Mark as used
368         gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
369         
370         // Mark used block
371         if(gaPageBitmap[ PAddr / 32 ] == -1)
372                 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
373         
374         // Release Spinlock
375         Mutex_Release( &glPhysAlloc );
376 }
377
378 /**
379  * \fn void MM_DerefPhys(tPAddr PAddr)
380  * \brief Dereferences a physical page
381  */
382 void MM_DerefPhys(tPAddr PAddr)
383 {
384         // Get page number
385         PAddr >>= 12;
386         
387         // We don't care about non-ram pages
388         if(PAddr >= giPageCount)        return;
389         
390         // Check if it is freed
391         if(gaPageReferences[ PAddr ] == 0) {
392                 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
393                 return;
394         }
395         
396         // Lock Structures
397         Mutex_Acquire( &glPhysAlloc );
398         
399         if( giLastPossibleFree < PAddr )
400                 giLastPossibleFree = PAddr;
401
402         // Dereference
403         gaPageReferences[ PAddr ] --;
404         
405         // Mark as free in bitmaps
406         if( gaPageReferences[ PAddr ] == 0 )
407         {
408                 #if TRACE_ALLOCS
409                 Log_Debug("PMem", "MM_DerefPhys: Free'd 0x%x (%i free)", PAddr, giPageCount-giPhysAlloc);
410                 #endif
411                 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
412                 giPhysAlloc --;
413                 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
414                 if(gaPageReferences[ PAddr ] == 0)
415                         gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
416         }
417         
418         // Release spinlock
419         Mutex_Release( &glPhysAlloc );
420 }
421
422 /**
423  * \fn int MM_GetRefCount(tPAddr Addr)
424  */
425 int MM_GetRefCount(tPAddr Addr)
426 {
427         // Get page number
428         Addr >>= 12;
429         
430         // We don't care about non-ram pages
431         if(Addr >= giPageCount) return -1;
432         
433         // Check if it is freed
434         return gaPageReferences[ Addr ];
435 }

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