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

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