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

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