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

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