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

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