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

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