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

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