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

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