Initial commit of kernel only
[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                 //if(gaPageBitmap[ num2 / 32 ] == 0) {
80                 //      memsetd(&gaPageReferences[num2], 0, 31-(num2&31));
81                 //      num2 = (num2 + 32) & ~31;
82                 //} else
83                         gaPageReferences[num] = (gaPageBitmap[ num / 32 ] >> (num&31)) & 1;
84         }
85 }
86
87 /**
88  * \fn Uint32 MM_AllocPhys()
89  * \brief Allocates a physical page
90  */
91 tPAddr MM_AllocPhys()
92 {
93          int    num = giPageCount / 32 / 32;
94          int    a, b, c;
95         Uint32  ret;
96         
97         LOCK( &giPhysAlloc );
98         
99         // Find free page
100         for(a=0;gaSuperBitmap[a]==-1&&a<num;a++);
101         if(a == num) {
102                 RELEASE( &giPhysAlloc );
103                 return 0;
104         }
105         for(b=0;gaSuperBitmap[a]&(1<<b);b++);
106         for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
107         //for(c=0;gaPageReferences[a*32*32+b*32+c]>0;c++);
108         
109         // Mark page used
110         if(gaPageReferences)
111                 gaPageReferences[a*32*32+b*32+c] = 1;
112         gaPageBitmap[ a*32+b ] |= 1 << c;
113         
114         // Get address
115         ret = (a << 22) + (b << 17) + (c << 12);
116         
117         // Mark used block
118         if(gaPageBitmap[ a*32+b ] == -1)        gaSuperBitmap[a] |= 1 << b;
119
120         // Release Spinlock
121         RELEASE( &giPhysAlloc );
122         //LOG("ret = %x", ret);
123         return ret;
124 }
125
126 /**
127  * \fn void MM_RefPhys(tPAddr Addr)
128  */
129 void MM_RefPhys(tPAddr Addr)
130 {
131         // Get page number
132         Addr >>= 12;
133         
134         // We don't care about non-ram pages
135         if(Addr >= giPageCount) return;
136         
137         // Lock Structures
138         LOCK( &giPhysAlloc );
139         
140         // Reference the page
141         if(gaPageReferences)
142                 gaPageReferences[ Addr ] ++;
143         
144         // Mark as used
145         gaPageBitmap[ Addr / 32 ] |= 1 << (Addr&31);
146         
147         // Mark used block
148         if(gaPageBitmap[ Addr / 32 ] == -1)     gaSuperBitmap[Addr/1024] |= 1 << ((Addr/32)&31);
149         
150         // Release Spinlock
151         RELEASE( &giPhysAlloc );
152 }
153
154 /**
155  * \fn void MM_DerefPhys(Uint32 Addr)
156  */
157 void MM_DerefPhys(Uint32 Addr)
158 {
159         // Get page number
160         Addr >>= 12;
161         
162         // We don't care about non-ram pages
163         if(Addr >= giPageCount) return;
164         
165         // Check if it is freed
166         if(gaPageReferences[ Addr ] == 0) {
167                 Warning("MM_DerefPhys - Non-referenced memory dereferenced");
168                 return;
169         }
170         
171         // Lock Structures
172         LOCK( &giPhysAlloc );
173         
174         // Dereference
175         gaPageReferences[ Addr ] --;
176         
177         // Mark as free in bitmaps
178         if( gaPageReferences[ Addr ] == 0 )
179         {
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 }

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