Extended MM_Dump to dump table addresses
[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 >> 22;
165                 page < End >> 22;
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         }
390         
391         
392         return *gTmpCR3;
393 }
394
395 /**
396  * \fn Uint MM_Clone()
397  * \brief Clone the current address space
398  */
399 Uint MM_Clone()
400 {
401         Uint    i, j;
402         Uint    page = 0;
403         Uint    kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
404         void    *tmp;
405         
406         //ENTER("");
407         
408         // Create Directory Table
409         *gTmpCR3 = MM_AllocPhys() | 3;
410         INVLPG( gaTmpDir );
411         //LOG("Allocated Directory (%x)", *gTmpCR3);
412         memsetd( gaTmpDir, 0, 1024 );
413         
414         // Copy Tables
415         for(i=0;i<768;i++)
416         {
417                 // Check if table is allocated
418                 if( !(gaPageDir[i] & PF_PRESENT) ) {
419                         gaTmpDir[i] = 0;
420                         page += 1024;
421                         continue;
422                 }
423                 
424                 // Allocate new table
425                 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
426                 INVLPG( &gaTmpTable[page] );
427                 // Fill
428                 for( j = 0; j < 1024; j ++, page++ )
429                 {
430                         if( !(gaPageTable[page] & PF_PRESENT) ) {
431                                 gaTmpTable[page] = 0;
432                                 continue;
433                         }
434                         
435                         // Refrence old page
436                         MM_RefPhys( gaPageTable[page] & ~0xFFF );
437                         // Add to new table
438                         if(gaPageTable[page] & PF_WRITE) {
439                                 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
440                                 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
441                                 INVLPG( page << 12 );
442                         }
443                         else
444                                 gaTmpTable[page] = gaPageTable[page];
445                 }
446         }
447         
448         // Map in kernel tables (and make fractal mapping)
449         for( i = 768; i < 1024; i ++ )
450         {
451                 // Fractal
452                 if( i == (PAGE_TABLE_ADDR >> 22) ) {
453                         gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
454                         continue;
455                 }
456                 
457                 if( gaPageDir[i] == 0 ) {
458                         gaTmpDir[i] = 0;
459                         continue;
460                 }
461                 
462                 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
463                 MM_RefPhys( gaPageDir[i] & ~0xFFF );
464                 gaTmpDir[i] = gaPageDir[i];
465         }
466         
467         // Allocate kernel stack
468         for(i = KERNEL_STACKS >> 22;
469                 i < KERNEL_STACK_END >> 22;
470                 i ++ )
471         {
472                 // Check if directory is allocated
473                 if( (gaPageDir[i] & 1) == 0 ) {
474                         gaTmpDir[i] = 0;
475                         continue;
476                 }               
477                 
478                 // We don't care about other kernel stacks, just the current one
479                 if( i != kStackBase >> 22 ) {
480                         MM_DerefPhys( gaPageDir[i] & ~0xFFF );
481                         gaTmpDir[i] = 0;
482                         continue;
483                 }
484                 
485                 // Create a copy
486                 gaTmpDir[i] = MM_AllocPhys() | 3;
487                 INVLPG( &gaTmpTable[i*1024] );
488                 for( j = 0; j < 1024; j ++ )
489                 {
490                         // Is the page allocated? If not, skip
491                         if( !(gaPageTable[i*1024+j] & 1) ) {
492                                 gaTmpTable[i*1024+j] = 0;
493                                 continue;
494                         }
495                         
496                         // We don't care about other kernel stacks
497                         if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
498                                 gaTmpTable[i*1024+j] = 0;
499                                 continue;
500                         }
501                         
502                         // Allocate page
503                         gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
504                         
505                         MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
506                         
507                         tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
508                         memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
509                         MM_FreeTemp( (Uint)tmp );
510                 }
511         }
512         
513         //LEAVE('x', *gTmpCR3 & ~0xFFF);
514         return *gTmpCR3 & ~0xFFF;
515 }
516
517 /**
518  * \fn Uint MM_NewKStack()
519  * \brief Create a new kernel stack
520  */
521 Uint MM_NewKStack()
522 {
523         Uint    base = KERNEL_STACKS;
524         Uint    i;
525         for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
526         {
527                 if(MM_GetPhysAddr(base) != 0)   continue;
528                 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
529                         MM_Allocate(base+i);
530                 }
531                 return base+KERNEL_STACK_SIZE;
532         }
533         Warning("MM_NewKStack - No address space left\n");
534         return 0;
535 }
536
537 /**
538  * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
539  * \brief Sets the flags on a page
540  */
541 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
542 {
543         tPAddr  *ent;
544         if( !(gaPageDir[VAddr >> 22] & 1) )     return ;
545         if( !(gaPageTable[VAddr >> 12] & 1) )   return ;
546         
547         ent = &gaPageTable[VAddr >> 12];
548         
549         // Read-Only
550         if( Mask & MM_PFLAG_RO )
551         {
552                 if( Flags & MM_PFLAG_RO )       *ent &= ~PF_WRITE;
553                 else    *ent |= PF_WRITE;
554         }
555         
556         // Kernel
557         if( Mask & MM_PFLAG_KERNEL )
558         {
559                 if( Flags & MM_PFLAG_KERNEL )   *ent &= ~PF_USER;
560                 else    *ent |= PF_USER;
561         }
562         
563         // Copy-On-Write
564         if( Mask & MM_PFLAG_COW )
565         {
566                 if( Flags & MM_PFLAG_COW ) {
567                         *ent &= ~PF_WRITE;
568                         *ent |= PF_COW;
569                 }
570                 else {
571                         *ent &= ~PF_COW;
572                         *ent |= PF_WRITE;
573                 }
574         }
575 }
576
577 /**
578  * \fn tPAddr MM_DuplicatePage(Uint VAddr)
579  * \brief Duplicates a virtual page to a physical one
580  */
581 tPAddr MM_DuplicatePage(Uint VAddr)
582 {
583         tPAddr  ret;
584         Uint    temp;
585          int    wasRO = 0;
586         
587         // Check if mapped
588         if( !(gaPageDir  [VAddr >> 22] & PF_PRESENT) )  return 0;
589         if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) )  return 0;
590         
591         // Page Align
592         VAddr &= ~0xFFF;
593         
594         // Allocate new page
595         ret = MM_AllocPhys();
596         
597         // Write-lock the page (to keep data constistent), saving its R/W state
598         wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
599         gaPageTable[VAddr >> 12] &= ~PF_WRITE;
600         INVLPG( VAddr );
601         
602         // Copy Data
603         temp = MM_MapTemp(ret);
604         memcpy( (void*)temp, (void*)VAddr, 0x1000 );
605         MM_FreeTemp(temp);
606         
607         // Restore Writeable status
608         if(!wasRO)      gaPageTable[VAddr >> 12] |= PF_WRITE;
609         INVLPG(VAddr);
610         
611         return ret;
612 }
613
614 /**
615  * \fn Uint MM_MapTemp(tPAddr PAddr)
616  * \brief Create a temporary memory mapping
617  * \todo Show Luigi Barone (C Lecturer) and see what he thinks
618  */
619 Uint MM_MapTemp(tPAddr PAddr)
620 {
621          int    i;
622         
623         //ENTER("XPAddr", PAddr);
624         
625         PAddr &= ~0xFFF;
626         
627         //LOG("gilTempMappings = %i", gilTempMappings);
628         
629         for(;;)
630         {
631                 LOCK( &gilTempMappings );
632                 
633                 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
634                 {
635                         // Check if page used
636                         if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1)        continue;
637                         // Mark as used
638                         gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
639                         INVLPG( TEMP_MAP_ADDR + (i << 12) );
640                         //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
641                         RELEASE( &gilTempMappings );
642                         return TEMP_MAP_ADDR + (i << 12);
643                 }
644                 RELEASE( &gilTempMappings );
645                 Threads_Yield();
646         }
647 }
648
649 /**
650  * \fn void MM_FreeTemp(Uint PAddr)
651  * \brief Free's a temp mapping
652  */
653 void MM_FreeTemp(Uint VAddr)
654 {
655          int    i = VAddr >> 12;
656         //ENTER("xVAddr", VAddr);
657         
658         if(i >= (TEMP_MAP_ADDR >> 12))
659                 gaPageTable[ i ] = 0;
660         
661         //LEAVE('-');
662 }
663
664 /**
665  * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
666  * \brief Allocates a contigous number of pages
667  */
668 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
669 {
670          int    i, j;
671         
672         PAddr &= ~0xFFF;
673         
674         // Scan List
675         for( i = 0; i < NUM_HW_PAGES; i ++ )
676         {               
677                 // Check if addr used
678                 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
679                         continue;
680                 
681                 // Check possible region
682                 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
683                 {
684                         // If there is an allocated page in the region we are testing, break
685                         if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 )    break;
686                 }
687                 // Is it all free?
688                 if( j == Number )
689                 {
690                         // Allocate
691                         for( j = 0; j < Number; j++ ) {
692                                 MM_RefPhys( PAddr + (j<<12) );
693                                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
694                         }
695                         return HW_MAP_ADDR + (i<<12);
696                 }
697         }
698         // If we don't find any, return NULL
699         return 0;
700 }
701
702 /**
703  * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
704  * \brief Unmap a hardware page
705  */
706 void MM_UnmapHWPage(Uint VAddr, Uint Number)
707 {
708          int    i, j;
709         // Sanity Check
710         if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX)     return;
711         
712         i = VAddr >> 12;
713         
714         LOCK( &gilTempMappings );       // Temp and HW share a directory, so they share a lock
715         
716         for( j = 0; j < Number; j++ )
717         {
718                 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
719                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
720         }
721         
722         RELEASE( &gilTempMappings );
723 }
724
725 // --- EXPORTS ---
726 EXPORT(MM_GetPhysAddr);
727 EXPORT(MM_Map);
728 //EXPORT(MM_Unmap);
729 EXPORT(MM_MapHWPage);
730 EXPORT(MM_UnmapHWPage);

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