Added debug to malloc, added caller to out of memory message
[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("ret = %x", ret);
120         return ret;
121 }
122
123 /**
124  * \fn void MM_RefPhys(tPAddr Addr)
125  */
126 void MM_RefPhys(tPAddr Addr)
127 {
128         // Get page number
129         Addr >>= 12;
130         
131         // We don't care about non-ram pages
132         if(Addr >= giPageCount) return;
133         
134         // Lock Structures
135         LOCK( &giPhysAlloc );
136         
137         // Reference the page
138         if(gaPageReferences)
139                 gaPageReferences[ Addr ] ++;
140         
141         // Mark as used
142         gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
143         
144         // Mark used block
145         if(gaPageBitmap[ Addr / 32 ] == -1)     gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
146         
147         // Release Spinlock
148         RELEASE( &giPhysAlloc );
149 }
150
151 /**
152  * \fn void MM_DerefPhys(Uint32 Addr)
153  */
154 void MM_DerefPhys(tPAddr Addr)
155 {
156         // Get page number
157         Addr >>= 12;
158         
159         // We don't care about non-ram pages
160         if(Addr >= giPageCount) return;
161         
162         // Check if it is freed
163         if(gaPageReferences[ Addr ] == 0) {
164                 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
165                 return;
166         }
167         
168         // Lock Structures
169         LOCK( &giPhysAlloc );
170         
171         // Dereference
172         gaPageReferences[ Addr ] --;
173         
174         // Mark as free in bitmaps
175         if( gaPageReferences[ Addr ] == 0 )
176         {
177                 gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
178                 if(gaPageReferences[ Addr ] == 0)
179                         gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));
180         }
181         
182         // Release spinlock
183         RELEASE( &giPhysAlloc );
184 }
185
186 /**
187  * \fn int MM_GetRefCount(tPAddr Addr)
188  */
189 int MM_GetRefCount(tPAddr Addr)
190 {
191         // Get page number
192         Addr >>= 12;
193         
194         // We don't care about non-ram pages
195         if(Addr >= giPageCount) return -1;
196         
197         // Check if it is freed
198         return gaPageReferences[ Addr ];
199 }

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