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

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