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

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