2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
17 #define KERNEL_STACKS 0xF0000000
18 #define KERNEL_STACK_SIZE 0x00002000
19 #define KERNEL_STACKS_END 0xFD000000
20 #define WORKER_STACKS 0x00100000 // Thread0 Only!
21 #define WORKER_STACK_SIZE KERNEL_STACK_SIZE
22 #define WORKER_STACKS_END 0xB0000000
23 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
24 #define PAGE_TABLE_ADDR 0xFD000000
25 #define PAGE_DIR_ADDR 0xFD3F4000
26 #define PAGE_CR3_ADDR 0xFD3F4FD0
27 #define TMP_CR3_ADDR 0xFD3F4FD4 // Part of core instead of temp
28 #define TMP_DIR_ADDR 0xFD3F5000 // Same
29 #define TMP_TABLE_ADDR 0xFD400000
30 #define HW_MAP_ADDR 0xFD800000
31 #define HW_MAP_MAX 0xFEFF0000
32 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
33 #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages
34 #define NUM_TEMP_PAGES 16
36 #define PF_PRESENT 0x1
40 #define PF_PAGED 0x400
42 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
45 extern Uint32 gaInitPageDir[1024];
46 extern Uint32 gaInitPageTable[1024];
47 extern void Threads_SegFault(tVAddr Addr);
48 extern void Error_Backtrace(Uint eip, Uint ebp);
51 void MM_PreinitVirtual();
52 void MM_InstallVirtual();
53 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
54 void MM_DumpTables(tVAddr Start, tVAddr End);
55 tPAddr MM_DuplicatePage(tVAddr VAddr);
58 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
59 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
60 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
61 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
62 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
63 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
64 int gilTempMappings = 0;
65 int gilTempFractal = 0;
66 Uint32 gWorkerStacks[NUM_WORKER_STACKS/32];
67 int giLastUsedWorker = 0;
71 * \fn void MM_PreinitVirtual()
72 * \brief Maps the fractal mappings
74 void MM_PreinitVirtual()
76 gaInitPageDir[ 0 ] = 0;
77 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
81 * \fn void MM_InstallVirtual()
82 * \brief Sets up the constant page mappings
84 void MM_InstallVirtual()
88 // --- Pre-Allocate kernel tables
89 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
91 if( gaPageDir[ i ] ) continue;
92 // Skip stack tables, they are process unique
93 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
98 gaPageDir[ i ] = MM_AllocPhys() | 3;
99 INVLPG( &gaPageTable[i*1024] );
100 memset( &gaPageTable[i*1024], 0, 0x1000 );
105 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
106 * \brief Called on a page fault
108 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
110 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
112 // -- Check for COW --
113 if( gaPageDir [Addr>>22] & PF_PRESENT
114 && gaPageTable[Addr>>12] & PF_PRESENT
115 && gaPageTable[Addr>>12] & PF_COW )
118 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
120 gaPageTable[Addr>>12] &= ~PF_COW;
121 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
125 paddr = MM_DuplicatePage( Addr );
126 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
127 gaPageTable[Addr>>12] &= PF_USER;
128 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
131 INVLPG( Addr & ~0xFFF );
136 // If it was a user, tell the thread handler
138 Warning("%s %s %s memory%s",
139 (ErrorCode&4?"User":"Kernel"),
140 (ErrorCode&2?"write to":"read from"),
141 (ErrorCode&1?"bad/locked":"non-present"),
142 (ErrorCode&16?" (Instruction Fetch)":"")
144 Warning("User Pagefault: Instruction at %p accessed %p", Regs->eip, Addr);
145 __asm__ __volatile__ ("sti"); // Restart IRQs
146 Threads_SegFault(Addr);
150 // -- Check Error Code --
152 Warning("Reserved Bits Trashed!");
155 Warning("%s %s %s memory%s",
156 (ErrorCode&4?"User":"Kernel"),
157 (ErrorCode&2?"write to":"read from"),
158 (ErrorCode&1?"bad/locked":"non-present"),
159 (ErrorCode&16?" (Instruction Fetch)":"")
163 Log("Code at %p accessed %p", Regs->eip, Addr);
164 // Print Stack Backtrace
165 Error_Backtrace(Regs->eip, Regs->ebp);
167 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
168 if( gaPageDir[Addr>>22] & PF_PRESENT )
169 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
171 MM_DumpTables(0, -1);
173 Panic("Page Fault at 0x%x\n", Regs->eip);
177 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
178 * \brief Dumps the layout of the page tables
180 void MM_DumpTables(tVAddr Start, tVAddr End)
182 tVAddr rangeStart = 0;
186 const tPAddr MASK = ~0xF98;
188 Start >>= 12; End >>= 12;
191 Log("Directory Entries:");
192 for(page = Start >> 10;
193 page < (End >> 10)+1;
198 Log(" 0x%08x-0x%08x :: 0x%08x",
199 page<<22, ((page+1)<<22)-1,
200 gaPageDir[page]&~0xFFF
206 Log("Table Entries:");
207 for(page = Start, curPos = Start<<12;
209 curPos += 0x1000, page++)
211 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
212 || !(gaPageTable[page] & PF_PRESENT)
213 || (gaPageTable[page] & MASK) != expected)
216 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
217 rangeStart, curPos - 1,
218 gaPageTable[rangeStart>>12] & ~0xFFF,
219 (expected & ~0xFFF) - 1,
220 (expected & PF_PAGED ? "p" : "-"),
221 (expected & PF_COW ? "C" : "-"),
222 (expected & PF_USER ? "U" : "-"),
223 (expected & PF_WRITE ? "W" : "-")
227 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
228 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
230 expected = (gaPageTable[page] & MASK);
233 if(expected) expected += 0x1000;
237 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
238 rangeStart, curPos - 1,
239 gaPageTable[rangeStart>>12] & ~0xFFF,
240 (expected & ~0xFFF) - 1,
241 (expected & PF_PAGED ? "p" : "-"),
242 (expected & PF_COW ? "C" : "-"),
243 (expected & PF_USER ? "U" : "-"),
244 (expected & PF_WRITE ? "W" : "-")
251 * \fn tPAddr MM_Allocate(tVAddr VAddr)
253 tPAddr MM_Allocate(tVAddr VAddr)
256 // Check if the directory is mapped
257 if( gaPageDir[ VAddr >> 22 ] == 0 )
259 // Allocate directory
260 paddr = MM_AllocPhys();
262 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
266 gaPageDir[ VAddr >> 22 ] = paddr | 3;
268 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
270 INVLPG( &gaPageDir[ VAddr >> 22 ] );
271 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
273 // Check if the page is already allocated
274 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
275 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
276 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
280 paddr = MM_AllocPhys();
282 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
283 VAddr, __builtin_return_address(0));
287 gaPageTable[ VAddr >> 12 ] = paddr | 3;
289 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
290 // Invalidate Cache for address
291 INVLPG( VAddr & ~0xFFF );
297 * \fn void MM_Deallocate(tVAddr VAddr)
299 void MM_Deallocate(tVAddr VAddr)
301 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
302 Warning("MM_Deallocate - Directory not mapped");
306 if(gaPageTable[ VAddr >> 12 ] == 0) {
307 Warning("MM_Deallocate - Page is not allocated");
312 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
314 gaPageTable[ VAddr >> 12 ] = 0;
318 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
319 * \brief Checks if the passed address is accesable
321 tPAddr MM_GetPhysAddr(tVAddr Addr)
323 if( !(gaPageDir[Addr >> 22] & 1) )
325 if( !(gaPageTable[Addr >> 12] & 1) )
327 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
332 * \fn int MM_IsUser(tVAddr Addr)
333 * \brief Checks if a page is user accessable
335 int MM_IsUser(tVAddr Addr)
337 if( !(gaPageDir[Addr >> 22] & 1) )
339 if( !(gaPageTable[Addr >> 12] & 1) )
341 if( !(gaPageTable[Addr >> 12] & PF_USER) )
347 * \fn void MM_SetCR3(tPAddr CR3)
348 * \brief Sets the current process space
350 void MM_SetCR3(tPAddr CR3)
352 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
356 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
357 * \brief Map a physical page to a virtual one
359 int MM_Map(tVAddr VAddr, tPAddr PAddr)
361 //ENTER("xVAddr xPAddr", VAddr, PAddr);
363 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
364 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
370 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
372 // Check if the directory is mapped
373 if( gaPageDir[ VAddr >> 22 ] == 0 )
375 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
378 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
380 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
381 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
383 // Check if the page is already allocated
384 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
385 Warning("MM_Map - Allocating to used address");
391 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
393 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
395 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
396 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
401 //LOG("INVLPG( 0x%x )", VAddr);
409 * \fn tVAddr MM_ClearUser()
410 * \brief Clear user's address space
412 tVAddr MM_ClearUser()
417 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
419 // Check if directory is not allocated
420 if( !(gaPageDir[i] & PF_PRESENT) ) {
426 for( j = 0; j < 1024; j ++ )
428 if( gaPageTable[i*1024+j] & 1 )
429 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
430 gaPageTable[i*1024+j] = 0;
433 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
435 INVLPG( &gaPageTable[i*1024] );
443 * \fn tPAddr MM_Clone()
444 * \brief Clone the current address space
451 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
454 LOCK( &gilTempFractal );
456 // Create Directory Table
457 *gTmpCR3 = MM_AllocPhys() | 3;
459 //LOG("Allocated Directory (%x)", *gTmpCR3);
460 memsetd( gaTmpDir, 0, 1024 );
465 // Check if table is allocated
466 if( !(gaPageDir[i] & PF_PRESENT) ) {
472 // Allocate new table
473 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
474 INVLPG( &gaTmpTable[page] );
476 for( j = 0; j < 1024; j ++, page++ )
478 if( !(gaPageTable[page] & PF_PRESENT) ) {
479 gaTmpTable[page] = 0;
484 MM_RefPhys( gaPageTable[page] & ~0xFFF );
486 if(gaPageTable[page] & PF_WRITE) {
487 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
488 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
489 INVLPG( page << 12 );
492 gaTmpTable[page] = gaPageTable[page];
496 // Map in kernel tables (and make fractal mapping)
497 for( i = 768; i < 1024; i ++ )
500 if( i == (PAGE_TABLE_ADDR >> 22) ) {
501 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
505 if( gaPageDir[i] == 0 ) {
510 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
511 MM_RefPhys( gaPageDir[i] & ~0xFFF );
512 gaTmpDir[i] = gaPageDir[i];
515 // Allocate kernel stack
516 for(i = KERNEL_STACKS >> 22;
517 i < KERNEL_STACKS_END >> 22;
520 // Check if directory is allocated
521 if( (gaPageDir[i] & 1) == 0 ) {
526 // We don't care about other kernel stacks, just the current one
527 if( i != kStackBase >> 22 ) {
528 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
534 gaTmpDir[i] = MM_AllocPhys() | 3;
535 INVLPG( &gaTmpTable[i*1024] );
536 for( j = 0; j < 1024; j ++ )
538 // Is the page allocated? If not, skip
539 if( !(gaPageTable[i*1024+j] & 1) ) {
540 gaTmpTable[i*1024+j] = 0;
544 // We don't care about other kernel stacks
545 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
546 gaTmpTable[i*1024+j] = 0;
551 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
553 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
555 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
556 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
557 MM_FreeTemp( (Uint)tmp );
561 ret = *gTmpCR3 & ~0xFFF;
562 RELEASE( &gilTempFractal );
569 * \fn tVAddr MM_NewKStack()
570 * \brief Create a new kernel stack
572 tVAddr MM_NewKStack()
574 tVAddr base = KERNEL_STACKS;
576 for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
578 if(MM_GetPhysAddr(base) != 0) continue;
579 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
582 return base+KERNEL_STACK_SIZE;
584 Warning("MM_NewKStack - No address space left\n");
589 * \fn tVAddr MM_NewWorkerStack()
590 * \brief Creates a new worker stack
592 tVAddr MM_NewWorkerStack()
599 tPAddr pages[WORKER_STACK_SIZE>>12];
601 // Get the old ESP and EBP
602 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
603 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
605 // Find a free worker stack address
606 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
609 if( gWorkerStacks[base/32] == -1 ) {
610 base += 31; base &= ~31;
611 base --; // Counteracted by the base++
615 if( gWorkerStacks[base/32] & (1 << base) ) {
619 if(base >= NUM_WORKER_STACKS) {
620 Warning("Uh-oh! Out of worker stacks");
625 gWorkerStacks[base/32] |= (1 << base);
626 // Make life easier for later calls
627 giLastUsedWorker = base;
629 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
631 // Acquire the lock for the temp fractal mappings
632 LOCK(&gilTempFractal);
634 // Set the temp fractals to TID0's address space
635 *gTmpCR3 = (Uint)gaInitPageDir | 3;
638 // Check if the directory is mapped (we are assuming that the stacks
639 // will fit neatly in a directory
640 if(gaTmpDir[ base >> 22 ] == 0) {
641 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
642 INVLPG( &gaTmpTable[ (base>>22) & ~0x3FF ] );
646 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
648 pages[ addr >> 12 ] = MM_AllocPhys();
649 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
651 // Release the temp mapping lock
652 RELEASE(&gilTempFractal);
654 // Copy the old stack
655 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
656 esp = oldstack - esp; // ESP as an offset in the stack
658 i = (WORKER_STACK_SIZE>>12) - 1;
659 // Copy the contents of the old stack to the new one, altering the addresses
660 // `addr` is refering to bytes from the stack base (mem downwards)
661 for(addr = 0; addr < esp; addr += 0x1000)
663 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
664 tmpPage = (void*)MM_MapTemp( pages[i] );
666 for(j = 0; j < 1024; j++)
668 // Possible Stack address?
669 if(oldstack-esp < stack[j] && stack[j] < oldstack)
670 tmpPage[j] = base - (oldstack - stack[j]);
671 else // Seems not, best leave it alone
672 tmpPage[j] = stack[j];
674 MM_FreeTemp((Uint)tmpPage);
682 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
683 * \brief Sets the flags on a page
685 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
688 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
689 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
691 ent = &gaPageTable[VAddr >> 12];
694 if( Mask & MM_PFLAG_RO )
696 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
697 else *ent |= PF_WRITE;
701 if( Mask & MM_PFLAG_KERNEL )
703 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
704 else *ent |= PF_USER;
708 if( Mask & MM_PFLAG_COW )
710 if( Flags & MM_PFLAG_COW ) {
722 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
723 * \brief Duplicates a virtual page to a physical one
725 tPAddr MM_DuplicatePage(tVAddr VAddr)
732 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
733 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
739 ret = MM_AllocPhys();
741 // Write-lock the page (to keep data constistent), saving its R/W state
742 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
743 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
747 temp = MM_MapTemp(ret);
748 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
751 // Restore Writeable status
752 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
759 * \fn Uint MM_MapTemp(tPAddr PAddr)
760 * \brief Create a temporary memory mapping
761 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
763 tVAddr MM_MapTemp(tPAddr PAddr)
767 //ENTER("XPAddr", PAddr);
771 //LOG("gilTempMappings = %i", gilTempMappings);
775 LOCK( &gilTempMappings );
777 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
779 // Check if page used
780 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
782 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
783 INVLPG( TEMP_MAP_ADDR + (i << 12) );
784 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
785 RELEASE( &gilTempMappings );
786 return TEMP_MAP_ADDR + (i << 12);
788 RELEASE( &gilTempMappings );
794 * \fn void MM_FreeTemp(tVAddr PAddr)
795 * \brief Free's a temp mapping
797 void MM_FreeTemp(tVAddr VAddr)
800 //ENTER("xVAddr", VAddr);
802 if(i >= (TEMP_MAP_ADDR >> 12))
803 gaPageTable[ i ] = 0;
809 * \fn tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number)
810 * \brief Allocates a contigous number of pages
812 tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number)
819 for( i = 0; i < NUM_HW_PAGES; i ++ )
821 // Check if addr used
822 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
825 // Check possible region
826 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
828 // If there is an allocated page in the region we are testing, break
829 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
835 for( j = 0; j < Number; j++ ) {
836 MM_RefPhys( PAddr + (j<<12) );
837 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
839 return HW_MAP_ADDR + (i<<12);
842 // If we don't find any, return NULL
847 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
848 * \brief Allocates DMA physical memory
849 * \param Pages Number of pages required
850 * \param MaxBits Maximum number of bits the physical address can have
851 * \param PhysAddr Pointer to the location to place the physical address allocated
852 * \return Virtual address allocate
854 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
856 tPAddr maxCheck = (1 << MaxBits);
860 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
863 if(MaxBits < 12 || !PhysAddr) {
869 if(MaxBits >= PHYS_BITS) maxCheck = -1;
872 if(Pages == 1 && MaxBits >= PHYS_BITS)
874 phys = MM_AllocPhys();
876 ret = MM_MapHWPage(phys, 1);
887 phys = MM_AllocPhysRange(Pages);
888 // - Was it allocated?
893 // - Check if the memory is OK
894 if(phys + (Pages-1)*0x1000 > maxCheck)
896 // Deallocate and return 0
897 for(;Pages--;phys+=0x1000)
903 // Allocated successfully, now map
904 ret = MM_MapHWPage(phys, Pages);
906 // If it didn't map, free then return 0
907 for(;Pages--;phys+=0x1000)
919 * \fn void MM_UnmapHWPage(tVAddr VAddr, Uint Number)
920 * \brief Unmap a hardware page
922 void MM_UnmapHWPage(tVAddr VAddr, Uint Number)
926 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
930 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
932 for( j = 0; j < Number; j++ )
934 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
935 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
938 RELEASE( &gilTempMappings );
942 EXPORT(MM_GetPhysAddr);
945 EXPORT(MM_MapHWPage);
947 EXPORT(MM_UnmapHWPage);