Initial commit of kernel only
[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 0xFD3F47F4
22 //#define TMP_CR3_ADDR  0xFD3F47F8      // Part of core instead of temp
23 #define PAGE_CR3_ADDR   0xFD3F4FD0
24 #define TMP_CR3_ADDR    0xFD3F4FD4      // Part of core instead of temp
25 #define TMP_DIR_ADDR    0xFD3F5000      // Same
26 #define TMP_TABLE_ADDR  0xFD400000
27 #define HW_MAP_ADDR             0xFD800000
28 #define HW_MAP_MAX              0xFEFF0000
29 #define NUM_HW_PAGES    ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
30 #define TEMP_MAP_ADDR   0xFEFF0000      // Allows 16 "temp" pages
31 #define NUM_TEMP_PAGES  16
32
33 #define USE_COW 1
34
35 #define PF_PRESENT      0x1
36 #define PF_WRITE        0x2
37 #define PF_USER         0x4
38 #define PF_COW          0x200
39 #define PF_PAGED        0x400
40
41 #define INVLPG(addr)    __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
42
43 // === IMPORTS ===
44 extern Uint32   gaInitPageDir[1024];
45 extern Uint32   gaInitPageTable[1024];
46
47 // === PROTOTYPES ===
48 void    MM_PreinitVirtual();
49 void    MM_InstallVirtual();
50 void    MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
51 void    MM_DumpTables(tVAddr Start, tVAddr End);
52 tPAddr  MM_DuplicatePage(Uint VAddr);
53
54 // === GLOBALS ===
55 tPAddr  *gaPageTable = (void*)PAGE_TABLE_ADDR;
56 tPAddr  *gaPageDir = (void*)PAGE_DIR_ADDR;
57 tPAddr  *gaPageCR3 = (void*)PAGE_CR3_ADDR;
58 tPAddr  *gaTmpTable = (void*)TMP_TABLE_ADDR;
59 tPAddr  *gaTmpDir = (void*)TMP_DIR_ADDR;
60 tPAddr  *gTmpCR3 = (void*)TMP_CR3_ADDR;
61  int    gilTempMappings = 0;
62
63 // === CODE ===
64 /**
65  * \fn void MM_PreinitVirtual()
66  */
67 void MM_PreinitVirtual()
68 {
69         gaInitPageDir[ 0 ] = 0;
70         gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
71 }
72 /**
73  * \fn void MM_InstallVirtual()
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                 paddr = MM_DuplicatePage( Addr );
110                 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
111                 gaPageTable[Addr>>12] &= PF_USER;
112                 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
113                 INVLPG( Addr & ~0xFFF );
114         }
115         
116         // -- Check Error Code --
117         if(ErrorCode & 8)
118                 Warning("Reserved Bits Trashed!");
119         else
120         {
121                 Warning("%s %s %s memory%s",
122                         (ErrorCode&4?"User":"Kernel"),
123                         (ErrorCode&2?"write to":"read from"),
124                         (ErrorCode&1?"bad/locked":"non-present"),
125                         (ErrorCode&16?" (Instruction Fetch)":"")
126                         );
127         }
128         
129         Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
130         if( gaPageDir[Addr>>22] & PF_PRESENT )
131                 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
132         
133         MM_DumpTables(0, -1);   
134         
135         Panic("Page Fault at 0x%x\n", Regs->eip);
136         LEAVE('-');
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");
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                         #if USE_COW
394                         // Refrence old page
395                         MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
396                         // Add to new table
397                         if(gaPageTable[i*1024+j] & PF_WRITE) {
398                                 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
399                                 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
400                         }
401                         else
402                                 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
403                         LOG("gaTmpTable[0x%x] = 0x%x", i*1024+j, gaTmpTable[i*1024+j]);
404                         #else
405                         gaTmpTable[i*1024+j] = MM_DuplicatePage( (i*1024+j)<<12 ) | (gaPageTable[i*1024+j]&7);
406                         #endif
407                 }
408         }
409         
410         // Map in kernel tables (and make fractal mapping)
411         for( i = 768; i < 1024; i ++ )
412         {
413                 // Fractal
414                 if( i == (PAGE_TABLE_ADDR >> 22) ) {
415                         gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
416                         continue;
417                 }
418                 
419                 if( gaPageDir[i] == 0 ) {
420                         gaTmpDir[i] = 0;
421                         continue;
422                 }
423                 
424                 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
425                 MM_RefPhys( gaPageDir[i] & ~0xFFF );
426                 gaTmpDir[i] = gaPageDir[i];
427         }
428         
429         // Allocate kernel stack
430         for(i = KERNEL_STACKS >> 22;
431                 i < KERNEL_STACK_END >> 22;
432                 i ++ )
433         {
434                 // Check if directory is allocated
435                 if( (gaPageDir[i] & 1) == 0 ) {
436                         gaTmpDir[i] = 0;
437                         continue;
438                 }               
439                 
440                 // We don't care about other kernel stacks, just the current one
441                 if( i != kStackBase >> 22 ) {
442                         MM_DerefPhys( gaPageDir[i] & ~0xFFF );
443                         gaTmpDir[i] = 0;
444                         continue;
445                 }
446                 
447                 // Create a copy
448                 gaTmpDir[i] = MM_AllocPhys() | 3;
449                 INVLPG( &gaTmpTable[i*1024] );
450                 for( j = 0; j < 1024; j ++ )
451                 {
452                         // Is the page allocated? If not, skip
453                         if( !(gaPageTable[i*1024+j] & 1) ) {
454                                 gaTmpTable[i*1024+j] = 0;
455                                 continue;
456                         }
457                         
458                         // We don't care about other kernel stacks
459                         if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
460                                 gaTmpTable[i*1024+j] = 0;
461                                 continue;
462                         }
463                         
464                         // Allocate page
465                         gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
466                         
467                         MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
468                         
469                         tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
470                         memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
471                         MM_FreeTemp( (Uint)tmp );
472                 }
473         }
474         
475         //LEAVE('x', *gTmpCR3 & ~0xFFF);
476         return *gTmpCR3 & ~0xFFF;
477 }
478
479 /**
480  * \fn Uint MM_NewKStack()
481  * \brief Create a new kernel stack
482  */
483 Uint MM_NewKStack()
484 {
485         Uint    base = KERNEL_STACKS;
486         Uint    i;
487         for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
488         {
489                 if(MM_GetPhysAddr(base) != 0)   continue;
490                 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
491                         MM_Allocate(base+i);
492                 }
493                 return base+KERNEL_STACK_SIZE;
494         }
495         Warning("MM_NewKStack - No address space left\n");
496         return 0;
497 }
498
499 /**
500  * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
501  * \brief Sets the flags on a page
502  */
503 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
504 {
505         tPAddr  *ent;
506         if( !(gaPageDir[VAddr >> 22] & 1) )     return ;
507         if( !(gaPageTable[VAddr >> 12] & 1) )   return ;
508         
509         ent = &gaPageTable[VAddr >> 12];
510         
511         // Read-Only
512         if( Mask & MM_PFLAG_RO )
513         {
514                 if( Flags & MM_PFLAG_RO )       *ent &= ~PF_WRITE;
515                 else    *ent |= PF_WRITE;
516         }
517         
518         // Kernel
519         if( Mask & MM_PFLAG_KERNEL )
520         {
521                 if( Flags & MM_PFLAG_KERNEL )   *ent &= ~PF_USER;
522                 else    *ent |= PF_USER;
523         }
524 }
525
526 /**
527  * \fn tPAddr MM_DuplicatePage(Uint VAddr)
528  * \brief Duplicates a virtual page to a physical one
529  */
530 tPAddr MM_DuplicatePage(Uint VAddr)
531 {
532         tPAddr  ret;
533         Uint    temp;
534          int    wasRO = 0;
535         
536         // Check if mapped
537         if( !(gaPageDir  [VAddr >> 22] & PF_PRESENT) )  return 0;
538         if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) )  return 0;
539         
540         // Page Align
541         VAddr &= ~0xFFF;
542         
543         // Allocate new page
544         ret = MM_AllocPhys();
545         
546         // Write-lock the page (to keep data constistent), saving its R/W state
547         wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
548         gaPageTable[VAddr >> 12] &= ~PF_WRITE;
549         INVLPG( VAddr );
550         
551         // Copy Data
552         temp = MM_MapTemp(ret);
553         memcpy( (void*)temp, (void*)VAddr, 0x1000 );
554         MM_FreeTemp(temp);
555         
556         // Restore Writeable status
557         if(!wasRO)      gaPageTable[VAddr >> 12] |= PF_WRITE;
558         INVLPG(VAddr);
559         
560         return ret;
561 }
562
563 /**
564  * \fn Uint MM_MapTemp(tPAddr PAddr)
565  * \brief Create a temporary memory mapping
566  * \todo Show Luigi Barone (C Lecturer) and see what he thinks
567  */
568 Uint MM_MapTemp(tPAddr PAddr)
569 {
570          int    i;
571         
572         //ENTER("XPAddr", PAddr);
573         
574         PAddr &= ~0xFFF;
575         
576         //LOG("gilTempMappings = %i", gilTempMappings);
577         
578         for(;;)
579         {
580                 LOCK( &gilTempMappings );
581                 
582                 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
583                 {
584                         // Check if page used
585                         if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1)        continue;
586                         // Mark as used
587                         gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
588                         INVLPG( TEMP_MAP_ADDR + (i << 12) );
589                         //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
590                         RELEASE( &gilTempMappings );
591                         return TEMP_MAP_ADDR + (i << 12);
592                 }
593                 RELEASE( &gilTempMappings );
594                 Proc_Yield();
595         }
596 }
597
598 /**
599  * \fn void MM_FreeTemp(Uint PAddr)
600  * \brief Free's a temp mapping
601  */
602 void MM_FreeTemp(Uint VAddr)
603 {
604          int    i = VAddr >> 12;
605         //ENTER("xVAddr", VAddr);
606         
607         if(i >= (TEMP_MAP_ADDR >> 12))
608                 gaPageTable[ i ] = 0;
609         
610         //LEAVE('-');
611 }
612
613 /**
614  * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
615  * \brief Allocates a contigous number of pages
616  */
617 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
618 {
619          int    i, j;
620         
621         PAddr &= ~0xFFF;
622         
623         // Scan List
624         for( i = 0; i < NUM_HW_PAGES; i ++ )
625         {               
626                 // Check if addr used
627                 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
628                         continue;
629                 
630                 // Check possible region
631                 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
632                 {
633                         // If there is an allocated page in the region we are testing, break
634                         if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 )    break;
635                 }
636                 // Is it all free?
637                 if( j == Number )
638                 {
639                         // Allocate
640                         for( j = 0; j < Number; j++ ) {
641                                 MM_RefPhys( PAddr + (j<<12) );
642                                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
643                         }
644                         return HW_MAP_ADDR + (i<<12);
645                 }
646         }
647         // If we don't find any, return NULL
648         return 0;
649 }
650
651 /**
652  * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
653  * \brief Unmap a hardware page
654  */
655 void MM_UnmapHWPage(Uint VAddr, Uint Number)
656 {
657          int    i, j;
658         // Sanity Check
659         if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX)     return;
660         
661         i = VAddr >> 12;
662         
663         LOCK( &gilTempMappings );       // Temp and HW share a directory, so they share a lock
664         
665         for( j = 0; j < Number; j++ )
666         {
667                 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
668                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
669         }
670         
671         RELEASE( &gilTempMappings );
672 }
673
674 // --- EXPORTS ---
675 EXPORT(MM_GetPhysAddr);
676 EXPORT(MM_Map);
677 //EXPORT(MM_Unmap);
678 EXPORT(MM_MapHWPage);
679 EXPORT(MM_UnmapHWPage);

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