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

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