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

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