Working on multiprocessing support
[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 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 (Kernel)
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(void)
123  * \brief Allocates a physical page from the general pool
124  */
125 tPAddr MM_AllocPhys(void)
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         ENTER("iPages iMaxBits", Pages, MaxBits);
211         
212         // Sanity Checks
213         if(MaxBits < 0) {
214                 LEAVE('i', 0);
215                 return 0;
216         }
217         if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
218         
219         // Lock
220         LOCK( &giPhysAlloc );
221         
222         // Set up search state
223         if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
224                 sidx = (tPAddr)1 << (MaxBits-12);
225         }
226         else {
227                 sidx = giLastPossibleFree;
228         }
229         idx = sidx / 32;
230         sidx %= 32;
231         b = idx % 32;
232         a = idx / 32;
233         
234         #if 0
235         LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
236         
237         // Find free page
238         for( ; gaSuperBitmap[a] == -1 && a --; )        b = 31;
239         if(a < 0) {
240                 RELEASE( &giPhysAlloc );
241                 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
242                 LEAVE('i', 0);
243                 return 0;
244         }
245         LOG("a = %i", a);
246         for( ; gaSuperBitmap[a] & (1 << b); b-- )       sidx = 31;
247         LOG("b = %i", b);
248         idx = a * 32 + b;
249         for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
250                 LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
251         
252         LOG("idx = %i, sidx = %i", idx, sidx);
253         #else
254         
255         #endif
256         
257         // Check if the gap is large enough
258         while( idx >= 0 )
259         {
260                 // Find a free page
261                 for( ; ; )
262                 {
263                         // Bulk Skip
264                         if( gaPageBitmap[idx] == -1 ) {
265                                 idx --;
266                                 sidx = 31;
267                                 continue;
268                         }
269                         
270                         if( gaPageBitmap[idx] & (1 << sidx) ) {
271                                 sidx --;
272                                 if(sidx < 0) {  sidx = 31;      idx --; }
273                                 if(idx < 0)     break;
274                                 continue;
275                         }
276                         break;
277                 }
278                 if( idx < 0 )   break;
279                 
280                 // Check if it is a free range
281                 for( i = 0; i < Pages; i++ )
282                 {
283                         // Used page? break
284                         if( gaPageBitmap[idx] & (1 << sidx) )
285                                 break;
286                         
287                         sidx --;
288                         if(sidx < 0) {  sidx = 31;      idx --; }
289                         if(idx < 0)     break;
290                 }
291                 
292                 if( i == Pages )
293                         break;
294         }
295         
296         // Check if an address was found
297         if( idx < 0 ) {
298                 RELEASE( &giPhysAlloc );
299                 Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
300                 LEAVE('i', 0);
301                 return 0;
302         }
303         
304         // Mark pages used
305         for( i = 0; i < Pages; i++ )
306         {
307                 if(gaPageReferences)
308                         gaPageReferences[idx*32+sidx] = 1;
309                 gaPageBitmap[ idx ] |= 1 << sidx;
310                 sidx ++;
311                 if(sidx == 32) { sidx = 0;      idx ++; }
312         }
313         
314         // Get address
315         ret = (idx << 17) | (sidx << 12);
316         
317         // Mark used block
318         if(gaPageBitmap[ idx ] == -1)   gaSuperBitmap[idx/32] |= 1 << (idx%32);
319
320         // Release Spinlock
321         RELEASE( &giPhysAlloc );
322         
323         LEAVE('X', ret);
324         return ret;
325 }
326
327 /**
328  * \fn void MM_RefPhys(tPAddr PAddr)
329  */
330 void MM_RefPhys(tPAddr PAddr)
331 {
332         // Get page number
333         PAddr >>= 12;
334         
335         // We don't care about non-ram pages
336         if(PAddr >= giPageCount)        return;
337         
338         // Lock Structures
339         LOCK( &giPhysAlloc );
340         
341         // Reference the page
342         if(gaPageReferences)
343                 gaPageReferences[ PAddr ] ++;
344         
345         // Mark as used
346         gaPageBitmap[ PAddr / 32 ] |= 1 << (PAddr&31);
347         
348         // Mark used block
349         if(gaPageBitmap[ PAddr / 32 ] == -1)
350                 gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31);
351         
352         // Release Spinlock
353         RELEASE( &giPhysAlloc );
354 }
355
356 /**
357  * \fn void MM_DerefPhys(tPAddr PAddr)
358  * \brief Dereferences a physical page
359  */
360 void MM_DerefPhys(tPAddr PAddr)
361 {
362         // Get page number
363         PAddr >>= 12;
364         
365         // We don't care about non-ram pages
366         if(PAddr >= giPageCount)        return;
367         
368         // Check if it is freed
369         if(gaPageReferences[ PAddr ] == 0) {
370                 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
371                 return;
372         }
373         
374         // Lock Structures
375         LOCK( &giPhysAlloc );
376         
377         if( giLastPossibleFree < PAddr )
378                 giLastPossibleFree = PAddr;
379
380         // Dereference
381         gaPageReferences[ PAddr ] --;
382         
383         // Mark as free in bitmaps
384         if( gaPageReferences[ PAddr ] == 0 )
385         {
386                 //LOG("Freed 0x%x by %p\n", PAddr<<12, __builtin_return_address(0));
387                 gaPageBitmap[ PAddr / 32 ] &= ~(1 << (PAddr&31));
388                 if(gaPageReferences[ PAddr ] == 0)
389                         gaSuperBitmap[ PAddr >> 10 ] &= ~(1 << ((PAddr >> 5)&31));
390         }
391         
392         // Release spinlock
393         RELEASE( &giPhysAlloc );
394 }
395
396 /**
397  * \fn int MM_GetRefCount(tPAddr Addr)
398  */
399 int MM_GetRefCount(tPAddr Addr)
400 {
401         // Get page number
402         Addr >>= 12;
403         
404         // We don't care about non-ram pages
405         if(Addr >= giPageCount) return -1;
406         
407         // Check if it is freed
408         return gaPageReferences[ Addr ];
409 }

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