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

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