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

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