Various Changes
[tpg/acess2.git] / Kernel / arch / x86 / mm_phys.c
1 /*
2  * Acess2
3  * - Physical memory manager
4  */
5 #define DEBUG   1
6 #include <common.h>
7 #include <mboot.h>
8 #include <mm_virt.h>
9
10 #define REFERENCE_BASE  0xE0400000
11
12 // === IMPORTS ===
13 extern void     gKernelEnd;
14
15 // === PROTOTYPES ===
16 Uint32  MM_AllocPhys();
17 void    MM_RefPhys(Uint32 Addr);
18 void    MM_DerefPhys(Uint32 Addr);
19
20 // === GLOBALS ===
21  int    giPhysAlloc = 0;
22 Uint    giPageCount = 0;
23 Uint32  gaSuperBitmap[1024];    // Blocks of 1024 Pages
24 Uint32  gaPageBitmap[1024*1024/32];     // Individual pages
25 Uint32  *gaPageReferences;
26
27 // === CODE ===
28 void MM_Install(tMBoot_Info *MBoot)
29 {
30         Uint    kernelPages, num;
31         Uint    i;
32         tMBoot_Module   *mods;
33         
34         // Initialise globals
35         giPageCount = (MBoot->HighMem >> 2) + 256;      // HighMem is a kByte value
36         LOG("giPageCount = %i", giPageCount);
37         
38         // Get used page count
39         kernelPages = (Uint)&gKernelEnd - KERNEL_BASE;
40         kernelPages += 0xFFF;   // Page Align
41         kernelPages >>= 12;
42         
43         // Fill page bitmap
44         num = kernelPages/32;
45         memsetd(gaPageBitmap, -1, num);
46         gaPageBitmap[ num ] = (1 << (kernelPages & 31)) - 1;
47         
48         // Fill Superpage bitmap
49         num = kernelPages/(32*32);
50         memsetd(gaSuperBitmap, -1, num);
51         gaSuperBitmap[ num ] = (1 << ((kernelPages / 32) & 31)) - 1;
52         
53         // Mark Multiboot's pages as taken
54         // - Structure
55         MM_RefPhys( (Uint)MBoot - KERNEL_BASE );
56         // - Module List
57         for(i = (MBoot->ModuleCount*sizeof(tMBoot_Module)+0xFFF)>12; i--; )
58                 MM_RefPhys( MBoot->Modules + (i << 12) );
59         // - Modules
60         mods = (void*)(MBoot->Modules + KERNEL_BASE);
61         for(i = 0; i < MBoot->ModuleCount; i++)
62         {
63                 num = (mods[i].End - mods[i].Start + 0xFFF) >> 12;
64                 while(num--)
65                         MM_RefPhys( (mods[i].Start & ~0xFFF) + (num<<12) );
66         }
67         
68         // Allocate References
69         LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
70         for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
71         {
72                 MM_Allocate( REFERENCE_BASE + (num<<12) );
73         }
74         
75         // Fill references
76         gaPageReferences = (void*)REFERENCE_BASE;
77         memsetd(gaPageReferences, 1, kernelPages);
78         for( num = kernelPages; num < giPageCount; num++ )
79         {
80                 gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
81         }
82 }
83
84 /**
85  * \fn tPAddr MM_AllocPhys()
86  * \brief Allocates a physical page
87  */
88 tPAddr MM_AllocPhys()
89 {
90          int    num = giPageCount / 32 / 32;
91          int    a, b, c;
92         Uint32  ret;
93         
94         LOCK( &giPhysAlloc );
95         
96         // Find free page
97         for(a=0;gaSuperBitmap[a]==-1&&a<num;a++);
98         if(a == num) {
99                 RELEASE( &giPhysAlloc );
100                 Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
101                 return 0;
102         }
103         for(b=0;gaSuperBitmap[a]&(1<<b);b++);
104         for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
105         //for(c=0;gaPageReferences[a*32*32+b*32+c]>0;c++);
106         
107         // Mark page used
108         if(gaPageReferences)
109                 gaPageReferences[a*32*32+b*32+c] = 1;
110         gaPageBitmap[ a*32+b ] |= 1 << c;
111         
112         // Get address
113         ret = (a << 22) + (b << 17) + (c << 12);
114         
115         // Mark used block
116         if(gaPageBitmap[ a*32+b ] == -1)        gaSuperBitmap[a] |= 1 << b;
117
118         // Release Spinlock
119         RELEASE( &giPhysAlloc );
120         //LOG("Allocated 0x%x\n", ret);
121         //LOG("ret = %x", ret);
122         return ret;
123 }
124
125 /**
126  * \fn void MM_RefPhys(tPAddr Addr)
127  */
128 void MM_RefPhys(tPAddr Addr)
129 {
130         // Get page number
131         Addr >>= 12;
132         
133         // We don't care about non-ram pages
134         if(Addr >= giPageCount) return;
135         
136         // Lock Structures
137         LOCK( &giPhysAlloc );
138         
139         // Reference the page
140         if(gaPageReferences)
141                 gaPageReferences[ Addr ] ++;
142         
143         // Mark as used
144         gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
145         
146         // Mark used block
147         if(gaPageBitmap[ Addr / 32 ] == -1)     gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
148         
149         // Release Spinlock
150         RELEASE( &giPhysAlloc );
151 }
152
153 /**
154  * \fn void MM_DerefPhys(Uint32 Addr)
155  */
156 void MM_DerefPhys(tPAddr Addr)
157 {
158         // Get page number
159         Addr >>= 12;
160         
161         // We don't care about non-ram pages
162         if(Addr >= giPageCount) return;
163         
164         // Check if it is freed
165         if(gaPageReferences[ Addr ] == 0) {
166                 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
167                 return;
168         }
169         
170         // Lock Structures
171         LOCK( &giPhysAlloc );
172         
173         // Dereference
174         gaPageReferences[ Addr ] --;
175         
176         // Mark as free in bitmaps
177         if( gaPageReferences[ Addr ] == 0 )
178         {
179                 //LOG("Freed 0x%x by %p\n", Addr<<12, __builtin_return_address(0));
180                 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
181                 if(gaPageReferences[ Addr ] == 0)
182                         gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
183         }
184         
185         // Release spinlock
186         RELEASE( &giPhysAlloc );
187 }
188
189 /**
190  * \fn int MM_GetRefCount(tPAddr Addr)
191  */
192 int MM_GetRefCount(tPAddr Addr)
193 {
194         // Get page number
195         Addr >>= 12;
196         
197         // We don't care about non-ram pages
198         if(Addr >= giPageCount) return -1;
199         
200         // Check if it is freed
201         return gaPageReferences[ Addr ];
202 }

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