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

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