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

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