7db7ceddea43437fd885abe39253dbd58ed5783d
[tpg/acess2.git] / Kernel / arch / x86 / mm_virt.c
1 /*
2  * AcessOS Microkernel Version
3  * mm_virt.c
4  * 
5  * Memory Map
6  * 0xE0 - Kernel Base
7  * 0xF0 - Kernel Stacks
8  * 0xFD - Fractals
9  * 0xFE - Unused
10  * 0xFF - System Calls / Kernel's User Code
11  */
12 #define DEBUG   1
13 #include <common.h>
14 #include <mm_phys.h>
15 #include <proc.h>
16
17 #define KERNEL_STACKS   0xF0000000
18 #define KERNEL_STACK_SIZE       0x00002000
19 #define KERNEL_STACK_END        0xFD000000
20 #define PAGE_TABLE_ADDR 0xFD000000
21 #define PAGE_DIR_ADDR   0xFD3F4000
22 #define PAGE_CR3_ADDR   0xFD3F4FD0
23 #define TMP_CR3_ADDR    0xFD3F4FD4      // Part of core instead of temp
24 #define TMP_DIR_ADDR    0xFD3F5000      // Same
25 #define TMP_TABLE_ADDR  0xFD400000
26 #define HW_MAP_ADDR             0xFD800000
27 #define HW_MAP_MAX              0xFEFF0000
28 #define NUM_HW_PAGES    ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
29 #define TEMP_MAP_ADDR   0xFEFF0000      // Allows 16 "temp" pages
30 #define NUM_TEMP_PAGES  16
31
32 #define PF_PRESENT      0x1
33 #define PF_WRITE        0x2
34 #define PF_USER         0x4
35 #define PF_COW          0x200
36 #define PF_PAGED        0x400
37
38 #define INVLPG(addr)    __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
39
40 // === IMPORTS ===
41 extern Uint32   gaInitPageDir[1024];
42 extern Uint32   gaInitPageTable[1024];
43
44 // === PROTOTYPES ===
45 void    MM_PreinitVirtual();
46 void    MM_InstallVirtual();
47 void    MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
48 void    MM_DumpTables(tVAddr Start, tVAddr End);
49 tPAddr  MM_DuplicatePage(Uint VAddr);
50
51 // === GLOBALS ===
52 tPAddr  *gaPageTable = (void*)PAGE_TABLE_ADDR;
53 tPAddr  *gaPageDir = (void*)PAGE_DIR_ADDR;
54 tPAddr  *gaPageCR3 = (void*)PAGE_CR3_ADDR;
55 tPAddr  *gaTmpTable = (void*)TMP_TABLE_ADDR;
56 tPAddr  *gaTmpDir = (void*)TMP_DIR_ADDR;
57 tPAddr  *gTmpCR3 = (void*)TMP_CR3_ADDR;
58  int    gilTempMappings = 0;
59
60 // === CODE ===
61 /**
62  * \fn void MM_PreinitVirtual()
63  * \brief Maps the fractal mappings
64  */
65 void MM_PreinitVirtual()
66 {
67         gaInitPageDir[ 0 ] = 0;
68         gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
69 }
70
71 /**
72  * \fn void MM_InstallVirtual()
73  * \brief Sets up the constant page mappings
74  */
75 void MM_InstallVirtual()
76 {
77          int    i;
78         
79         // --- Pre-Allocate kernel tables
80         for( i = KERNEL_BASE>>22; i < 1024; i ++ )
81         {
82                 if( gaPageDir[ i ] )    continue;
83                 // Skip stack tables, they are process unique
84                 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACK_END >> 22) {
85                         gaPageDir[ i ] = 0;
86                         continue;
87                 }
88                 // Preallocate table
89                 gaPageDir[ i ] = MM_AllocPhys() | 3;
90                 INVLPG( &gaPageTable[i*1024] );
91                 memset( &gaPageTable[i*1024], 0, 0x1000 );
92         }
93 }
94
95 /**
96  * \fn void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
97  * \brief Called on a page fault
98  */
99 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
100 {
101         //ENTER("xAddr bErrorCode", Addr, ErrorCode);
102         
103         // -- Check for COW --
104         if( gaPageDir  [Addr>>22] & PF_PRESENT
105          && gaPageTable[Addr>>12] & PF_PRESENT
106          && gaPageTable[Addr>>12] & PF_COW )
107         {
108                 tPAddr  paddr;
109                 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
110                 {
111                         gaPageTable[Addr>>12] &= ~PF_COW;
112                         gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
113                 }
114                 else
115                 {
116                         paddr = MM_DuplicatePage( Addr );
117                         MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
118                         gaPageTable[Addr>>12] &= PF_USER;
119                         gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
120                 }
121                 //LOG("Duplicated page at %p to 0x%x", Addr&~0xFFF, gaPageTable[Addr>>12]);
122                 INVLPG( Addr & ~0xFFF );
123                 //LEAVE('-')
124                 return;
125         }
126         
127         // -- Check Error Code --
128         if(ErrorCode & 8)
129                 Warning("Reserved Bits Trashed!");
130         else
131         {
132                 Warning("%s %s %s memory%s",
133                         (ErrorCode&4?"User":"Kernel"),
134                         (ErrorCode&2?"write to":"read from"),
135                         (ErrorCode&1?"bad/locked":"non-present"),
136                         (ErrorCode&16?" (Instruction Fetch)":"")
137                         );
138         }
139         
140         Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
141         if( gaPageDir[Addr>>22] & PF_PRESENT )
142                 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
143         
144         MM_DumpTables(0, -1);   
145         
146         Panic("Page Fault at 0x%x\n", Regs->eip);
147 }
148
149 /**
150  * \fn void MM_DumpTables(Uint Start, Uint End)
151  * \brief Dumps the layout of the page tables
152  */
153 void MM_DumpTables(tVAddr Start, tVAddr End)
154 {
155         tVAddr  rangeStart = 0;
156         tPAddr  expected = 0;
157         tVAddr  curPos;
158         Uint    page;
159         const tPAddr    MASK = ~0xF98;
160         
161         Start >>= 12;   End >>= 12;
162         
163         Log("Directory Entries:");
164         for(page = Start >> 10;
165                 page < (End >> 10)+1;
166                 page ++)
167         {
168                 if(gaPageDir[page])
169                 {
170                         Log(" 0x%08x-0x%08x :: 0x%08x",
171                                 page<<22, ((page+1)<<22)-1,
172                                 gaPageDir[page]&~0xFFF
173                                 );
174                 }
175         }
176         
177         Log("Table Entries:");
178         for(page = Start, curPos = Start<<12;
179                 page < End;
180                 curPos += 0x1000, page++)
181         {
182                 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
183                 ||  !(gaPageTable[page] & PF_PRESENT)
184                 ||  (gaPageTable[page] & MASK) != expected)
185                 {
186                         if(expected) {
187                                 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
188                                         rangeStart, curPos - 1,
189                                         gaPageTable[rangeStart>>12] & ~0xFFF,
190                                         (expected & ~0xFFF) - 1,
191                                         (expected & PF_PAGED ? "p" : "-"),
192                                         (expected & PF_COW ? "C" : "-"),
193                                         (expected & PF_USER ? "U" : "-"),
194                                         (expected & PF_WRITE ? "W" : "-")
195                                         );
196                                 expected = 0;
197                         }
198                         if( !(gaPageDir[curPos>>22] & PF_PRESENT) )     continue;
199                         if( !(gaPageTable[curPos>>12] & PF_PRESENT) )   continue;
200                         
201                         expected = (gaPageTable[page] & MASK);
202                         rangeStart = curPos;
203                 }
204                 if(expected)    expected += 0x1000;
205         }
206         
207         if(expected) {
208                 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
209                         rangeStart, curPos - 1,
210                         gaPageTable[rangeStart>>12] & ~0xFFF,
211                         (expected & ~0xFFF) - 1,
212                         (expected & PF_PAGED ? "p" : "-"),
213                         (expected & PF_COW ? "C" : "-"),
214                         (expected & PF_USER ? "U" : "-"),
215                         (expected & PF_WRITE ? "W" : "-")
216                         );
217                 expected = 0;
218         }
219 }
220
221 /**
222  * \fn tPAddr MM_Allocate(Uint VAddr)
223  */
224 tPAddr MM_Allocate(Uint VAddr)
225 {
226         tPAddr  paddr;
227         // Check if the directory is mapped
228         if( gaPageDir[ VAddr >> 22 ] == 0 )
229         {
230                 // Allocate directory
231                 paddr = MM_AllocPhys();
232                 if( paddr == 0 ) {
233                         Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
234                         return 0;
235                 }
236                 // Map
237                 gaPageDir[ VAddr >> 22 ] = paddr | 3;
238                 // Mark as user
239                 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
240                 
241                 INVLPG( &gaPageDir[ VAddr >> 22 ] );
242                 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
243         }
244         // Check if the page is already allocated
245         else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
246                 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
247                 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
248         }
249         
250         // Allocate
251         paddr = MM_AllocPhys();
252         if( paddr == 0 ) {
253                 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
254                         VAddr, __builtin_return_address(0));
255                 return 0;
256         }
257         // Map
258         gaPageTable[ VAddr >> 12 ] = paddr | 3;
259         // Mark as user
260         if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
261         // Invalidate Cache for address
262         INVLPG( VAddr & ~0xFFF );
263         
264         return paddr;
265 }
266
267 /**
268  * \fn void MM_Deallocate(Uint VAddr)
269  */
270 void MM_Deallocate(Uint VAddr)
271 {
272         if( gaPageDir[ VAddr >> 22 ] == 0 ) {
273                 Warning("MM_Deallocate - Directory not mapped");
274                 return;
275         }
276         
277         if(gaPageTable[ VAddr >> 12 ] == 0) {
278                 Warning("MM_Deallocate - Page is not allocated");
279                 return;
280         }
281         
282         // Dereference page
283         MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
284         // Clear page
285         gaPageTable[ VAddr >> 12 ] = 0;
286 }
287
288 /**
289  * \fn tPAddr MM_GetPhysAddr(Uint Addr)
290  * \brief Checks if the passed address is accesable
291  */
292 tPAddr MM_GetPhysAddr(Uint Addr)
293 {
294         if( !(gaPageDir[Addr >> 22] & 1) )
295                 return 0;
296         if( !(gaPageTable[Addr >> 12] & 1) )
297                 return 0;
298         return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
299 }
300
301 /**
302  * \fn void MM_SetCR3(Uint CR3)
303  * \brief Sets the current process space
304  */
305 void MM_SetCR3(Uint CR3)
306 {
307         __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
308 }
309
310 /**
311  * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
312  * \brief Map a physical page to a virtual one
313  */
314 int MM_Map(Uint VAddr, tPAddr PAddr)
315 {
316         //ENTER("xVAddr xPAddr", VAddr, PAddr);
317         // Sanity check
318         if( PAddr & 0xFFF || VAddr & 0xFFF ) {
319                 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
320                 //LEAVE('i', 0);
321                 return 0;
322         }
323         
324         // Align addresses
325         PAddr &= ~0xFFF;        VAddr &= ~0xFFF;
326         
327         // Check if the directory is mapped
328         if( gaPageDir[ VAddr >> 22 ] == 0 )
329         {
330                 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
331                 
332                 // Mark as user
333                 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
334                 
335                 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
336                 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
337         }
338         // Check if the page is already allocated
339         else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
340                 Warning("MM_Map - Allocating to used address");
341                 //LEAVE('i', 0);
342                 return 0;
343         }
344         
345         // Map
346         gaPageTable[ VAddr >> 12 ] = PAddr | 3;
347         // Mark as user
348         if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
349         
350         //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
351         //      VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
352         
353         // Reference
354         MM_RefPhys( PAddr );
355         
356         //LOG("INVLPG( 0x%x )", VAddr);
357         INVLPG( VAddr );
358         
359         //LEAVE('i', 1);
360         return 1;
361 }
362
363 /**
364  * \fn Uint MM_ClearUser()
365  * \brief Clear user's address space
366  */
367 Uint MM_ClearUser()
368 {
369         Uint    i, j;
370         
371         // Copy Directories
372         for( i = 0; i < (MM_USER_MAX>>22); i ++ )
373         {
374                 // Check if directory is not allocated
375                 if( !(gaPageDir[i] & PF_PRESENT) ) {
376                         gaPageDir[i] = 0;
377                         continue;
378                 }
379                 
380                 
381                 for( j = 0; j < 1024; j ++ )
382                 {
383                         if( gaPageTable[i*1024+j] & 1 )
384                                 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
385                         gaPageTable[i*1024+j] = 0;
386                 }
387                 
388                 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
389                 gaPageDir[i] = 0;
390                 INVLPG( &gaPageTable[i*1024] );
391         }
392         INVLPG( gaPageDir );
393         
394         return *gaPageCR3;
395 }
396
397 /**
398  * \fn Uint MM_Clone()
399  * \brief Clone the current address space
400  */
401 Uint MM_Clone()
402 {
403         Uint    i, j;
404         Uint    page = 0;
405         Uint    kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
406         void    *tmp;
407         
408         //ENTER("");
409         
410         // Create Directory Table
411         *gTmpCR3 = MM_AllocPhys() | 3;
412         INVLPG( gaTmpDir );
413         //LOG("Allocated Directory (%x)", *gTmpCR3);
414         memsetd( gaTmpDir, 0, 1024 );
415         
416         // Copy Tables
417         for(i=0;i<768;i++)
418         {
419                 // Check if table is allocated
420                 if( !(gaPageDir[i] & PF_PRESENT) ) {
421                         gaTmpDir[i] = 0;
422                         page += 1024;
423                         continue;
424                 }
425                 
426                 // Allocate new table
427                 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
428                 INVLPG( &gaTmpTable[page] );
429                 // Fill
430                 for( j = 0; j < 1024; j ++, page++ )
431                 {
432                         if( !(gaPageTable[page] & PF_PRESENT) ) {
433                                 gaTmpTable[page] = 0;
434                                 continue;
435                         }
436                         
437                         // Refrence old page
438                         MM_RefPhys( gaPageTable[page] & ~0xFFF );
439                         // Add to new table
440                         if(gaPageTable[page] & PF_WRITE) {
441                                 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
442                                 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
443                                 INVLPG( page << 12 );
444                         }
445                         else
446                                 gaTmpTable[page] = gaPageTable[page];
447                 }
448         }
449         
450         // Map in kernel tables (and make fractal mapping)
451         for( i = 768; i < 1024; i ++ )
452         {
453                 // Fractal
454                 if( i == (PAGE_TABLE_ADDR >> 22) ) {
455                         gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
456                         continue;
457                 }
458                 
459                 if( gaPageDir[i] == 0 ) {
460                         gaTmpDir[i] = 0;
461                         continue;
462                 }
463                 
464                 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
465                 MM_RefPhys( gaPageDir[i] & ~0xFFF );
466                 gaTmpDir[i] = gaPageDir[i];
467         }
468         
469         // Allocate kernel stack
470         for(i = KERNEL_STACKS >> 22;
471                 i < KERNEL_STACK_END >> 22;
472                 i ++ )
473         {
474                 // Check if directory is allocated
475                 if( (gaPageDir[i] & 1) == 0 ) {
476                         gaTmpDir[i] = 0;
477                         continue;
478                 }               
479                 
480                 // We don't care about other kernel stacks, just the current one
481                 if( i != kStackBase >> 22 ) {
482                         MM_DerefPhys( gaPageDir[i] & ~0xFFF );
483                         gaTmpDir[i] = 0;
484                         continue;
485                 }
486                 
487                 // Create a copy
488                 gaTmpDir[i] = MM_AllocPhys() | 3;
489                 INVLPG( &gaTmpTable[i*1024] );
490                 for( j = 0; j < 1024; j ++ )
491                 {
492                         // Is the page allocated? If not, skip
493                         if( !(gaPageTable[i*1024+j] & 1) ) {
494                                 gaTmpTable[i*1024+j] = 0;
495                                 continue;
496                         }
497                         
498                         // We don't care about other kernel stacks
499                         if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
500                                 gaTmpTable[i*1024+j] = 0;
501                                 continue;
502                         }
503                         
504                         // Allocate page
505                         gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
506                         
507                         MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
508                         
509                         tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
510                         memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
511                         MM_FreeTemp( (Uint)tmp );
512                 }
513         }
514         
515         //LEAVE('x', *gTmpCR3 & ~0xFFF);
516         return *gTmpCR3 & ~0xFFF;
517 }
518
519 /**
520  * \fn Uint MM_NewKStack()
521  * \brief Create a new kernel stack
522  */
523 Uint MM_NewKStack()
524 {
525         Uint    base = KERNEL_STACKS;
526         Uint    i;
527         for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
528         {
529                 if(MM_GetPhysAddr(base) != 0)   continue;
530                 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
531                         MM_Allocate(base+i);
532                 }
533                 return base+KERNEL_STACK_SIZE;
534         }
535         Warning("MM_NewKStack - No address space left\n");
536         return 0;
537 }
538
539 /**
540  * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
541  * \brief Sets the flags on a page
542  */
543 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
544 {
545         tPAddr  *ent;
546         if( !(gaPageDir[VAddr >> 22] & 1) )     return ;
547         if( !(gaPageTable[VAddr >> 12] & 1) )   return ;
548         
549         ent = &gaPageTable[VAddr >> 12];
550         
551         // Read-Only
552         if( Mask & MM_PFLAG_RO )
553         {
554                 if( Flags & MM_PFLAG_RO )       *ent &= ~PF_WRITE;
555                 else    *ent |= PF_WRITE;
556         }
557         
558         // Kernel
559         if( Mask & MM_PFLAG_KERNEL )
560         {
561                 if( Flags & MM_PFLAG_KERNEL )   *ent &= ~PF_USER;
562                 else    *ent |= PF_USER;
563         }
564         
565         // Copy-On-Write
566         if( Mask & MM_PFLAG_COW )
567         {
568                 if( Flags & MM_PFLAG_COW ) {
569                         *ent &= ~PF_WRITE;
570                         *ent |= PF_COW;
571                 }
572                 else {
573                         *ent &= ~PF_COW;
574                         *ent |= PF_WRITE;
575                 }
576         }
577 }
578
579 /**
580  * \fn tPAddr MM_DuplicatePage(Uint VAddr)
581  * \brief Duplicates a virtual page to a physical one
582  */
583 tPAddr MM_DuplicatePage(Uint VAddr)
584 {
585         tPAddr  ret;
586         Uint    temp;
587          int    wasRO = 0;
588         
589         // Check if mapped
590         if( !(gaPageDir  [VAddr >> 22] & PF_PRESENT) )  return 0;
591         if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) )  return 0;
592         
593         // Page Align
594         VAddr &= ~0xFFF;
595         
596         // Allocate new page
597         ret = MM_AllocPhys();
598         
599         // Write-lock the page (to keep data constistent), saving its R/W state
600         wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
601         gaPageTable[VAddr >> 12] &= ~PF_WRITE;
602         INVLPG( VAddr );
603         
604         // Copy Data
605         temp = MM_MapTemp(ret);
606         memcpy( (void*)temp, (void*)VAddr, 0x1000 );
607         MM_FreeTemp(temp);
608         
609         // Restore Writeable status
610         if(!wasRO)      gaPageTable[VAddr >> 12] |= PF_WRITE;
611         INVLPG(VAddr);
612         
613         return ret;
614 }
615
616 /**
617  * \fn Uint MM_MapTemp(tPAddr PAddr)
618  * \brief Create a temporary memory mapping
619  * \todo Show Luigi Barone (C Lecturer) and see what he thinks
620  */
621 Uint MM_MapTemp(tPAddr PAddr)
622 {
623          int    i;
624         
625         //ENTER("XPAddr", PAddr);
626         
627         PAddr &= ~0xFFF;
628         
629         //LOG("gilTempMappings = %i", gilTempMappings);
630         
631         for(;;)
632         {
633                 LOCK( &gilTempMappings );
634                 
635                 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
636                 {
637                         // Check if page used
638                         if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1)        continue;
639                         // Mark as used
640                         gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
641                         INVLPG( TEMP_MAP_ADDR + (i << 12) );
642                         //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
643                         RELEASE( &gilTempMappings );
644                         return TEMP_MAP_ADDR + (i << 12);
645                 }
646                 RELEASE( &gilTempMappings );
647                 Threads_Yield();
648         }
649 }
650
651 /**
652  * \fn void MM_FreeTemp(Uint PAddr)
653  * \brief Free's a temp mapping
654  */
655 void MM_FreeTemp(Uint VAddr)
656 {
657          int    i = VAddr >> 12;
658         //ENTER("xVAddr", VAddr);
659         
660         if(i >= (TEMP_MAP_ADDR >> 12))
661                 gaPageTable[ i ] = 0;
662         
663         //LEAVE('-');
664 }
665
666 /**
667  * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
668  * \brief Allocates a contigous number of pages
669  */
670 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
671 {
672          int    i, j;
673         
674         PAddr &= ~0xFFF;
675         
676         // Scan List
677         for( i = 0; i < NUM_HW_PAGES; i ++ )
678         {               
679                 // Check if addr used
680                 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
681                         continue;
682                 
683                 // Check possible region
684                 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
685                 {
686                         // If there is an allocated page in the region we are testing, break
687                         if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 )    break;
688                 }
689                 // Is it all free?
690                 if( j == Number )
691                 {
692                         // Allocate
693                         for( j = 0; j < Number; j++ ) {
694                                 MM_RefPhys( PAddr + (j<<12) );
695                                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
696                         }
697                         return HW_MAP_ADDR + (i<<12);
698                 }
699         }
700         // If we don't find any, return NULL
701         return 0;
702 }
703
704 /**
705  * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
706  * \brief Unmap a hardware page
707  */
708 void MM_UnmapHWPage(Uint VAddr, Uint Number)
709 {
710          int    i, j;
711         // Sanity Check
712         if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX)     return;
713         
714         i = VAddr >> 12;
715         
716         LOCK( &gilTempMappings );       // Temp and HW share a directory, so they share a lock
717         
718         for( j = 0; j < Number; j++ )
719         {
720                 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
721                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
722         }
723         
724         RELEASE( &gilTempMappings );
725 }
726
727 // --- EXPORTS ---
728 EXPORT(MM_GetPhysAddr);
729 EXPORT(MM_Map);
730 //EXPORT(MM_Unmap);
731 EXPORT(MM_MapHWPage);
732 EXPORT(MM_UnmapHWPage);

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