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

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