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

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